<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="utf-8"><title>Angular - 生命周期钩子</title><meta name="Description" content="Angular is a platform for building mobile and desktop web applications.
    Join the community of millions of developers who build compelling user interfaces with Angular."><base href="/"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="search" type="application/opensearchdescription+xml" href="assets/opensearch.xml"><link rel="icon" type="image/x-icon" href="assets/images/favicons/favicon.ico"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-32x32.png" sizes="32x32"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-194x194.png" sizes="194x194"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-96x96.png" sizes="96x96"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-16x16.png" sizes="16x16"><link rel="apple-touch-icon" sizes="144x144" href="assets/images/favicons/favicon-144x144.png"><link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/images/favicons/favicon-144x144.png"><link href="assets/fonts/Material_Icons.css" rel="stylesheet"><link href="assets/fonts/Droid_Sans_Mono.css" rel="stylesheet"><link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"><link rel="manifest" href="pwa-manifest.json"><meta name="theme-color" content="#1976d2"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="translucent"><script async="" src="assets/js/analytics.js"></script><script>!function(e,a,n,t,s,c,g){e.GoogleAnalyticsObject=s,e.ga=e.ga||function(){(e.ga.q=e.ga.q||[]).push(arguments)},e.ga.l=1*new Date,c=a.createElement(n),g=a.getElementsByTagName(n)[0],c.async=1,c.src="assets/js/analytics.js",~e.name.indexOf("NG_DEFER_BOOTSTRAP")||g.parentNode.insertBefore(c,g)}(window,document,"script",0,"ga")</script><script>window.onerror=function(){ga("send","exception",{exDescription:function(e,r,n,a,c){var l;e=e.replace(/^Error: /,""),l=c?c.stack.replace(/^Error: /,"").replace(e+"\n","").replace(/^ +/gm,"").replace(/^at /gm,"").replace(/(?: \(|@)http.+\/([^/)]+)\)?(?:\n|$)/gm,"@$1\n").replace(/ *\(eval code(:\d+:\d+)\)(?:\n|$)/gm,"@???$1\n"):r+":"+(n=n||"?")+":"+(a=a||"?");return(e+"\n"+l).substr(0,150)}.apply(null,arguments),exFatal:!0})}</script><link rel="stylesheet" href="styles.4adbe52ad34df01b5273.css"><style>.cdk-high-contrast-active .mat-toolbar{outline:solid 1px}.mat-toolbar-row,.mat-toolbar-single-row{display:flex;box-sizing:border-box;padding:0 16px;width:100%;flex-direction:row;align-items:center;white-space:nowrap}.mat-toolbar-multiple-rows{display:flex;box-sizing:border-box;flex-direction:column;width:100%}.mat-toolbar-multiple-rows{min-height:64px}.mat-toolbar-row,.mat-toolbar-single-row{height:64px}@media(max-width:599px){.mat-toolbar-multiple-rows{min-height:56px}.mat-toolbar-row,.mat-toolbar-single-row{height:56px}}</style><style>.mat-icon{background-repeat:no-repeat;display:inline-block;fill:currentColor;height:24px;width:24px}.mat-icon.mat-icon-inline{font-size:inherit;height:inherit;line-height:inherit;width:inherit}[dir=rtl] .mat-icon-rtl-mirror{transform:scale(-1,1)}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon{display:block}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon-button .mat-icon,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon-button .mat-icon{margin:auto}</style><style>.mat-button .mat-button-focus-overlay,.mat-icon-button .mat-button-focus-overlay{opacity:0}.mat-button:hover .mat-button-focus-overlay,.mat-stroked-button:hover .mat-button-focus-overlay{opacity:.04}@media(hover:none){.mat-button:hover .mat-button-focus-overlay,.mat-stroked-button:hover .mat-button-focus-overlay{opacity:0}}.mat-button,.mat-flat-button,.mat-icon-button,.mat-stroked-button{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible}.mat-button::-moz-focus-inner,.mat-flat-button::-moz-focus-inner,.mat-icon-button::-moz-focus-inner,.mat-stroked-button::-moz-focus-inner{border:0}.mat-button[disabled],.mat-flat-button[disabled],.mat-icon-button[disabled],.mat-stroked-button[disabled]{cursor:default}.mat-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-button.cdk-program-focused .mat-button-focus-overlay,.mat-flat-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-flat-button.cdk-program-focused .mat-button-focus-overlay,.mat-icon-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-icon-button.cdk-program-focused .mat-button-focus-overlay,.mat-stroked-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-stroked-button.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-button::-moz-focus-inner,.mat-flat-button::-moz-focus-inner,.mat-icon-button::-moz-focus-inner,.mat-stroked-button::-moz-focus-inner{border:0}.mat-raised-button{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible;transform:translate3d(0,0,0);transition:background .4s cubic-bezier(.25,.8,.25,1),box-shadow 280ms cubic-bezier(.4,0,.2,1)}.mat-raised-button::-moz-focus-inner{border:0}.mat-raised-button[disabled]{cursor:default}.mat-raised-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-raised-button.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-raised-button::-moz-focus-inner{border:0}._mat-animation-noopable.mat-raised-button{transition:none;animation:none}.mat-stroked-button{border:1px solid currentColor;padding:0 15px;line-height:34px}.mat-stroked-button .mat-button-focus-overlay,.mat-stroked-button .mat-button-ripple.mat-ripple{top:-1px;left:-1px;right:-1px;bottom:-1px}.mat-fab{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible;transform:translate3d(0,0,0);transition:background .4s cubic-bezier(.25,.8,.25,1),box-shadow 280ms cubic-bezier(.4,0,.2,1);min-width:0;border-radius:50%;width:56px;height:56px;padding:0;flex-shrink:0}.mat-fab::-moz-focus-inner{border:0}.mat-fab[disabled]{cursor:default}.mat-fab.cdk-keyboard-focused .mat-button-focus-overlay,.mat-fab.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-fab::-moz-focus-inner{border:0}._mat-animation-noopable.mat-fab{transition:none;animation:none}.mat-fab .mat-button-wrapper{padding:16px 0;display:inline-block;line-height:24px}.mat-mini-fab{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible;transform:translate3d(0,0,0);transition:background .4s cubic-bezier(.25,.8,.25,1),box-shadow 280ms cubic-bezier(.4,0,.2,1);min-width:0;border-radius:50%;width:40px;height:40px;padding:0;flex-shrink:0}.mat-mini-fab::-moz-focus-inner{border:0}.mat-mini-fab[disabled]{cursor:default}.mat-mini-fab.cdk-keyboard-focused .mat-button-focus-overlay,.mat-mini-fab.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-mini-fab::-moz-focus-inner{border:0}._mat-animation-noopable.mat-mini-fab{transition:none;animation:none}.mat-mini-fab .mat-button-wrapper{padding:8px 0;display:inline-block;line-height:24px}.mat-icon-button{padding:0;min-width:0;width:40px;height:40px;flex-shrink:0;line-height:40px;border-radius:50%}.mat-icon-button .mat-icon,.mat-icon-button i{line-height:24px}.mat-button-focus-overlay,.mat-button-ripple.mat-ripple{top:0;left:0;right:0;bottom:0;position:absolute;pointer-events:none;border-radius:inherit}.mat-button-ripple.mat-ripple:not(:empty){transform:translateZ(0)}.mat-button-focus-overlay{opacity:0;transition:opacity .2s cubic-bezier(.35,0,.25,1),background-color .2s cubic-bezier(.35,0,.25,1)}._mat-animation-noopable .mat-button-focus-overlay{transition:none}.cdk-high-contrast-active .mat-button-focus-overlay{background-color:#fff}.cdk-high-contrast-black-on-white .mat-button-focus-overlay{background-color:#000}.mat-button-ripple-round{border-radius:50%;z-index:1}.mat-button .mat-button-wrapper>*,.mat-fab .mat-button-wrapper>*,.mat-flat-button .mat-button-wrapper>*,.mat-icon-button .mat-button-wrapper>*,.mat-mini-fab .mat-button-wrapper>*,.mat-raised-button .mat-button-wrapper>*,.mat-stroked-button .mat-button-wrapper>*{vertical-align:middle}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon-button,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon-button{display:block;font-size:inherit;width:2.5em;height:2.5em}.cdk-high-contrast-active .mat-button,.cdk-high-contrast-active .mat-fab,.cdk-high-contrast-active .mat-flat-button,.cdk-high-contrast-active .mat-icon-button,.cdk-high-contrast-active .mat-mini-fab,.cdk-high-contrast-active .mat-raised-button{outline:solid 1px}</style><style>.mat-drawer-container{position:relative;z-index:1;box-sizing:border-box;-webkit-overflow-scrolling:touch;display:block;overflow:hidden}.mat-drawer-container[fullscreen]{top:0;left:0;right:0;bottom:0;position:absolute}.mat-drawer-container[fullscreen].mat-drawer-container-has-open{overflow:hidden}.mat-drawer-container.mat-drawer-container-explicit-backdrop .mat-drawer-side{z-index:3}.mat-drawer-container.ng-animate-disabled .mat-drawer-backdrop,.mat-drawer-container.ng-animate-disabled .mat-drawer-content,.ng-animate-disabled .mat-drawer-container .mat-drawer-backdrop,.ng-animate-disabled .mat-drawer-container .mat-drawer-content{transition:none}.mat-drawer-backdrop{top:0;left:0;right:0;bottom:0;position:absolute;display:block;z-index:3;visibility:hidden}.mat-drawer-backdrop.mat-drawer-shown{visibility:visible}.mat-drawer-transition .mat-drawer-backdrop{transition-duration:.4s;transition-timing-function:cubic-bezier(.25,.8,.25,1);transition-property:background-color,visibility}.cdk-high-contrast-active .mat-drawer-backdrop{opacity:.5}.mat-drawer-content{position:relative;z-index:1;display:block;height:100%;overflow:auto}.mat-drawer-transition .mat-drawer-content{transition-duration:.4s;transition-timing-function:cubic-bezier(.25,.8,.25,1);transition-property:transform,margin-left,margin-right}.mat-drawer{position:relative;z-index:4;display:block;position:absolute;top:0;bottom:0;z-index:3;outline:0;box-sizing:border-box;overflow-y:auto;transform:translate3d(-100%,0,0)}.cdk-high-contrast-active .mat-drawer,.cdk-high-contrast-active [dir=rtl] .mat-drawer.mat-drawer-end{border-right:solid 1px currentColor}.cdk-high-contrast-active .mat-drawer.mat-drawer-end,.cdk-high-contrast-active [dir=rtl] .mat-drawer{border-left:solid 1px currentColor;border-right:none}.mat-drawer.mat-drawer-side{z-index:2}.mat-drawer.mat-drawer-end{right:0;transform:translate3d(100%,0,0)}[dir=rtl] .mat-drawer{transform:translate3d(100%,0,0)}[dir=rtl] .mat-drawer.mat-drawer-end{left:0;right:auto;transform:translate3d(-100%,0,0)}.mat-drawer-inner-container{width:100%;height:100%;overflow:auto;-webkit-overflow-scrolling:touch}.mat-sidenav-fixed{position:fixed}</style><style>.nav-link.highlight[_ngcontent-ng-docs-c30]{color:#ff0}</style><style>.mat-progress-bar{display:block;height:4px;overflow:hidden;position:relative;transition:opacity 250ms linear;width:100%}._mat-animation-noopable.mat-progress-bar{transition:none;animation:none}.mat-progress-bar .mat-progress-bar-element,.mat-progress-bar .mat-progress-bar-fill::after{height:100%;position:absolute;width:100%}.mat-progress-bar .mat-progress-bar-background{width:calc(100% + 10px)}.cdk-high-contrast-active .mat-progress-bar .mat-progress-bar-background{display:none}.mat-progress-bar .mat-progress-bar-buffer{transform-origin:top left;transition:transform 250ms ease}.cdk-high-contrast-active .mat-progress-bar .mat-progress-bar-buffer{border-top:solid 5px;opacity:.5}.mat-progress-bar .mat-progress-bar-secondary{display:none}.mat-progress-bar .mat-progress-bar-fill{animation:none;transform-origin:top left;transition:transform 250ms ease}.cdk-high-contrast-active .mat-progress-bar .mat-progress-bar-fill{border-top:solid 4px}.mat-progress-bar .mat-progress-bar-fill::after{animation:none;content:"";display:inline-block;left:0}.mat-progress-bar[dir=rtl],[dir=rtl] .mat-progress-bar{transform:rotateY(180deg)}.mat-progress-bar[mode=query]{transform:rotateZ(180deg)}.mat-progress-bar[mode=query][dir=rtl],[dir=rtl] .mat-progress-bar[mode=query]{transform:rotateZ(180deg) rotateY(180deg)}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-fill,.mat-progress-bar[mode=query] .mat-progress-bar-fill{transition:none}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-primary,.mat-progress-bar[mode=query] .mat-progress-bar-primary{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-primary-indeterminate-translate 2s infinite linear;left:-145.166611%}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-primary.mat-progress-bar-fill::after,.mat-progress-bar[mode=query] .mat-progress-bar-primary.mat-progress-bar-fill::after{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-primary-indeterminate-scale 2s infinite linear}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-secondary,.mat-progress-bar[mode=query] .mat-progress-bar-secondary{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-secondary-indeterminate-translate 2s infinite linear;left:-54.888891%;display:block}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-secondary.mat-progress-bar-fill::after,.mat-progress-bar[mode=query] .mat-progress-bar-secondary.mat-progress-bar-fill::after{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-secondary-indeterminate-scale 2s infinite linear}.mat-progress-bar[mode=buffer] .mat-progress-bar-background{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-background-scroll 250ms infinite linear;display:block}.mat-progress-bar._mat-animation-noopable .mat-progress-bar-background,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-buffer,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-fill,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-fill::after,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-primary,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-primary.mat-progress-bar-fill::after,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-secondary,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-secondary.mat-progress-bar-fill::after{animation:none;transition:none}@keyframes mat-progress-bar-primary-indeterminate-translate{0%{transform:translateX(0)}20%{animation-timing-function:cubic-bezier(.5,0,.701732,.495819);transform:translateX(0)}59.15%{animation-timing-function:cubic-bezier(.302435,.381352,.55,.956352);transform:translateX(83.67142%)}100%{transform:translateX(200.611057%)}}@keyframes mat-progress-bar-primary-indeterminate-scale{0%{transform:scaleX(.08)}36.65%{animation-timing-function:cubic-bezier(.334731,.12482,.785844,1);transform:scaleX(.08)}69.15%{animation-timing-function:cubic-bezier(.06,.11,.6,1);transform:scaleX(.661479)}100%{transform:scaleX(.08)}}@keyframes mat-progress-bar-secondary-indeterminate-translate{0%{animation-timing-function:cubic-bezier(.15,0,.515058,.409685);transform:translateX(0)}25%{animation-timing-function:cubic-bezier(.31033,.284058,.8,.733712);transform:translateX(37.651913%)}48.35%{animation-timing-function:cubic-bezier(.4,.627035,.6,.902026);transform:translateX(84.386165%)}100%{transform:translateX(160.277782%)}}@keyframes mat-progress-bar-secondary-indeterminate-scale{0%{animation-timing-function:cubic-bezier(.15,0,.515058,.409685);transform:scaleX(.08)}19.15%{animation-timing-function:cubic-bezier(.31033,.284058,.8,.733712);transform:scaleX(.457104)}44.15%{animation-timing-function:cubic-bezier(.4,.627035,.6,.902026);transform:scaleX(.72796)}100%{transform:scaleX(.08)}}@keyframes mat-progress-bar-background-scroll{to{transform:translateX(-8px)}}</style><script charset="utf-8" src="toc-toc-module-es2015.f8531f591ee147aebcc6.js"></script><script charset="utf-8" src="default~code-code-example-module~code-code-tabs-module-es2015.9efcc76d2979a2e605e0.js"></script><script charset="utf-8" src="code-code-example-module-es2015.fa05e6f700453813d256.js"></script><script charset="utf-8" src="live-example-live-example-module-es2015.d3b124043392e69f9e97.js"></script><script charset="utf-8" src="assets-js-prettify-js-es2015.ba856648f2dd2d930a9b.js"></script></head><body><aio-shell ng-version="9.0.0-rc.11" class="aio-notification-hide folder-guide mode-stable page-guide-lifecycle-hooks sidenav-open view-SideNav"><div id="top-of-page"></div><mat-toolbar color="primary" class="mat-toolbar app-toolbar no-print mat-primary mat-toolbar-multiple-rows"><mat-toolbar-row class="mat-toolbar-row notification-container"><aio-notification notificationid="survey-february-2019" expirationdate="2019-03-01" class="ng-tns-c22-0 ng-trigger ng-trigger-hideAnimation" style="height:0"><span class="content ng-tns-c22-0"><a href="http://bit.ly/angular-survey-2019" target="_blank" class="ng-tns-c22-0"><mat-icon role="img" svgicon="insert_comment" aria-label="Announcement" class="mat-icon notranslate icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M20 2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h14l4 4V4c0-1.1-.9-2-2-2zm-2 12H6v-2h12v2zm0-3H6V9h12v2zm0-3H6V6h12v2z"></path><path d="M0 0h24v24H0z" fill="none"></path></svg></mat-icon><span class="message"><b>填写这份《一分钟调查》</b>，帮我们（开发组）做得更好！</span><span class="action-button">去填写</span></a></span><button mat-icon-button="" aria-label="Close" class="close-button ng-tns-c22-0 mat-icon-button mat-button-base"><span class="mat-button-wrapper"><mat-icon role="img" svgicon="close" aria-label="Dismiss notification" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path><path d="M0 0h24v24H0z" fill="none"></path></svg></mat-icon></span><div matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></div><div class="mat-button-focus-overlay"></div></button></aio-notification></mat-toolbar-row><mat-toolbar-row class="mat-toolbar-row"><button mat-button="" title="Docs menu" class="hamburger mat-button mat-button-base"><span class="mat-button-wrapper"><mat-icon role="img" svgicon="menu" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"></path></svg></mat-icon></span><div matripple="" class="mat-ripple mat-button-ripple"></div><div class="mat-button-focus-overlay"></div></button><a href="/" class="nav-link home"><img src="assets/images/logos/angular/logo-nav@2x.png" width="150" height="40" title="Home" alt="Home" class="ng-star-inserted"></a><aio-top-menu _nghost-ng-docs-c30="" class="ng-star-inserted"><ul _ngcontent-ng-docs-c30="" role="navigation"><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="features" title="特性" target="_self"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">特性</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="docs" title="文档" target="_self"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">文档</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="resources" title="资源" target="_self"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">资源</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="events" title="会议" target="_self"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">会议</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="https://blog.wangke.io/" title="译者汪志成的博客，包括翻译文章和原创文章" target="_blank"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">译者博客</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="translations/cn/home" title="关于中文版" target="_self"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">关于中文版</span></a></li></ul></aio-top-menu><aio-search-box class="search-container"><input type="search" aria-label="search" placeholder="搜索"></aio-search-box><div class="toolbar-external-icons-container"><a href="https://twitter.com/angular" title="Twitter" aria-label="Angular on twitter"><mat-icon role="img" svgicon="logos:twitter" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 50 59" xmlns="http://www.w3.org/2000/svg"><path d="M50,9.3c-1.8,0.8-3.8,1.4-5.9,1.6c2.1-1.3,3.7-3.3,4.5-5.7c-2,1.2-4.2,2-6.5,2.5c-1.9-2-4.5-3.2-7.5-3.2c-5.7,0-10.3,4.6-10.3,10.3c0,0.8,0.1,1.6,0.3,2.3C16.1,16.7,8.5,12.6,3.5,6.4c-0.9,1.5-1.4,3.3-1.4,5.2c0,3.6,1.8,6.7,4.6,8.5C5,20,3.4,19.6,2,18.8c0,0,0,0.1,0,0.1c0,5,3.5,9.1,8.2,10.1c-0.9,0.2-1.8,0.4-2.7,0.4c-0.7,0-1.3-0.1-1.9-0.2c1.3,4.1,5.1,7,9.6,7.1c-3.5,2.8-7.9,4.4-12.7,4.4c-0.8,0-1.6,0-2.4-0.1c4.5,2.9,9.9,4.6,15.7,4.6c18.9,0,29.2-15.6,29.2-29.2c0-0.4,0-0.9,0-1.3C46.9,13.2,48.6,11.4,50,9.3z"></path></svg></mat-icon></a><a href="https://github.com/angular/angular" title="GitHub" aria-label="Angular on github"><mat-icon role="img" svgicon="logos:github" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 51.8 50.4" xmlns="http://www.w3.org/2000/svg"><path d="M25.9,0.2C11.8,0.2,0.3,11.7,0.3,25.8c0,11.3,7.3,20.9,17.5,24.3c1.3,0.2,1.7-0.6,1.7-1.2c0-0.6,0-2.6,0-4.8c-7.1,1.5-8.6-3-8.6-3c-1.2-3-2.8-3.7-2.8-3.7c-2.3-1.6,0.2-1.6,0.2-1.6c2.6,0.2,3.9,2.6,3.9,2.6c2.3,3.9,6,2.8,7.5,2.1c0.2-1.7,0.9-2.8,1.6-3.4c-5.7-0.6-11.7-2.8-11.7-12.7c0-2.8,1-5.1,2.6-6.9c-0.3-0.7-1.1-3.3,0.3-6.8c0,0,2.1-0.7,7,2.6c2-0.6,4.2-0.9,6.4-0.9c2.2,0,4.4,0.3,6.4,0.9c4.9-3.3,7-2.6,7-2.6c1.4,3.5,0.5,6.1,0.3,6.8c1.6,1.8,2.6,4.1,2.6,6.9c0,9.8-6,12-11.7,12.6c0.9,0.8,1.7,2.4,1.7,4.7c0,3.4,0,6.2,0,7c0,0.7,0.5,1.5,1.8,1.2c10.2-3.4,17.5-13,17.5-24.3C51.5,11.7,40.1,0.2,25.9,0.2z"></path></svg></mat-icon></a></div></mat-toolbar-row></mat-toolbar><mat-sidenav-container role="main" class="mat-drawer-container mat-sidenav-container sidenav-container mat-drawer-transition has-floating-toc"><div class="mat-drawer-backdrop ng-star-inserted"></div><div class="cdk-visually-hidden cdk-focus-trap-anchor" aria-hidden="true"></div><mat-sidenav tabindex="-1" class="mat-drawer mat-sidenav sidenav ng-tns-c18-1 ng-trigger ng-trigger-transform mat-drawer-side ng-star-inserted mat-drawer-opened" style="transform:none;visibility:visible"><div class="mat-drawer-inner-container ng-tns-c18-1"><aio-nav-menu class="ng-tns-c18-1"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="docs" title="Angular 文档简介" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="通过构建第一个 Angular 应用来学习基础知识" aria-pressed="false"><span>快速上手</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start" title="介绍 Angular 的组件模型、模板语法和组件通讯" target="_self"><span>你的第一个应用</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/routing" title="介绍如何使用浏览器的 URL 在组件之间进行路由" target="_self"><span>路由</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/data" title="介绍服务以及如何访问外部数据" target="_self"><span>管理数据</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/forms" title="学习如何使用表单从用户那里获取并管理数据" target="_self"><span>表单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/deployment" title="通过你的应用放到 Firebase 或自己的服务器上来把它分享给外界" target="_self"><span>部署</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="guide/setup-local" title="使用 Angular CLI 搭建本地开发环境简介" target="_self"><span>搭建环境</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 expanded selected ng-star-inserted" title="Angular 的基本原理" aria-pressed="true"><span>基本原理</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 expanded selected"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="介绍 Angular 应用中的一些基本概念。" aria-pressed="false"><span>Angular 的基本概念</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture" title="Angular 应用中的基本构造块。" target="_self"><span>基本概念简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-modules" title="关于 Angular 模块（NgModules）。" target="_self"><span>模块简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-components" title="关于组件、模板和视图。" target="_self"><span>组件简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-services" title="关于服务与依赖注入。" target="_self"><span>服务与 DI 简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-next-steps" title="超越基础阶段。" target="_self"><span>后续步骤</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="英雄指南是这里大量 Angular 范例的基础" aria-pressed="false"><span>英雄指南</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial" title="《英雄指南》教程简介" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt0" title="创建应用的外壳" target="_self"><span>应用的“外壳”</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt1" title="第一部分：构建一个简单的英雄编辑器" target="_self"><span>1. 英雄编辑器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt2" title="第二部分：构建一个主从结构的页面，用于展现英雄列表。" target="_self"><span>2. 显示英雄列表</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt3" title="第三部分：把主从结构的视图重构成几个独立的组件。" target="_self"><span>3. 创建特性组件</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt4" title="第四部分：创建一个可复用的服务来管理英雄数据。" target="_self"><span>4. 添加服务</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt5" title="第五部分：添加 Angular 路由器，并且学习在视图之间导航。" target="_self"><span>5. 添加应用内导航</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt6" title="第六部分：通过 HTTP 来获取并保存英雄数据。" target="_self"><span>6. 从服务器获取数据</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 expanded selected ng-star-inserted" title="通过数据绑定构建动态视图" aria-pressed="true"><span>组件与模板</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 expanded selected"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/displaying-data" title="属性绑定可以帮助应用把数据显示在界面上" target="_self"><span>显示数据</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/template-syntax" title="学习如何写模板，以便借助数据绑定机制显示数据并响应事件。" target="_self"><span>模板语法</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/user-input" title="用户输入会触发 DOM 事件。Angular 会通过事件绑定来监听那些事件，并把修改后的值传回应用的组件和模型中。" target="_self"><span>用户输入</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 expanded selected ng-star-inserted" style="position:relative" href="guide/lifecycle-hooks" title="Angular 调用指令和组件的生命周期钩子函数，包括它的创建、变更和销毁时。" target="_self"><span>生命周期钩子</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/component-interaction" title="在不同的指令和组件之间共享信息" target="_self"><span>组件交互</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/component-styles" title="添加专属于某个组件的样式" target="_self"><span>组件样式</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dynamic-component-loader" title="动态加载组件" target="_self"><span>动态组件</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/elements" title="把组件转换成自定义元素" target="_self"><span>Angular 元素</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/attribute-directives" title="属性型指令把行为添加到现有元素上。" target="_self"><span>属性型指令</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/structural-directives" title="结构型指令可以操纵页面的布局" target="_self"><span>结构型指令</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/pipes" title="管道可以在模板中转换显示的内容。" target="_self"><span>管道</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular 的表单" aria-pressed="false"><span>表单</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/forms-overview" title="表单可以创建集中、高效、引人注目的输入体验。Angular 表单可以协调一组数据绑定控件，跟踪变更，验证输入，并表达错误信息。" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/reactive-forms" title="使用 FormBuilder、表单组和表单数组创建响应式表单。" target="_self"><span>响应式表单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/forms" title="使用指令和 Angular 模板语法创建模板驱动表单。" target="_self"><span>模板驱动表单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/form-validation" title="验证用户的表单输入" target="_self"><span>表单验证</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dynamic-form" title="使用 FormGroup 渲染动态表单。" target="_self"><span>动态表单</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Observable 与 RxJS" aria-pressed="false"><span>Observable 与 RxJS</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/observables" title="" target="_self"><span>可观察对象(Observable)</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/rx-library" title="" target="_self"><span>RxJS 库</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/observables-in-angular" title="" target="_self"><span>Angular 中的可观察对象</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/practical-observable-usage" title="" target="_self"><span>用法实战</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/comparing-observables" title="" target="_self"><span>与其它技术的比较</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular 中的模块" aria-pressed="false"><span>NgModules</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodules" title="使用 NgModule 让你的应用更高效" target="_self"><span>NgModule 简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodule-vs-jsmodule" title="JavaScript 模块和 NgModule 之间的差异" target="_self"><span>JS 模块 vs NgModule</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/bootstrapping" title="告诉 Angular 如何在根 &quot;AppModule&quot; 中构造和引导应用。" target="_self"><span>应用的根模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/frequent-ngmodules" title="介绍最常用的 NgModule" target="_self"><span>常用模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/module-types" title="介绍特性模块的几种类型" target="_self"><span>特性模块的分类</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/entry-components" title="关于 Angular 中入口组件的一切" target="_self"><span>入口组件</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/feature-modules" title="创建特性模块，以组织你的代码" target="_self"><span>特性模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/providers" title="服务提供者与 NgModule" target="_self"><span>服务提供者</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/singleton-services" title="创建单例服务" target="_self"><span>单例服务</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/lazy-loading-ngmodules" title="惰性加载模块，以提高应用的性能" target="_self"><span>惰性加载的特性模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/sharing-ngmodules" title="共享 NgModule 让你的应用现代化。" target="_self"><span>共享 NgModule</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodule-api" title="理解 NgModule 的那些细节。" target="_self"><span>NgModule API</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodule-faq" title="回答关于 NgModules 的常见问题。" target="_self"><span>NgModule 常见问题</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="依赖注入：创建并注入各种服务。" aria-pressed="false"><span>依赖注入</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection" title="Angular 的依赖注入系统能够为 Angular 创建的类创建并交付它们所依赖的服务。" target="_self"><span>Angular 依赖注入</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/hierarchical-dependency-injection" title="与组件树平行的注入器树，并支持嵌套的依赖。" target="_self"><span>多级注入器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection-providers" title="各种提供者类型的更多知识。" target="_self"><span>DI 提供者</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection-in-action" title="依赖注入的使用技巧" target="_self"><span>DI 实战</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection-navtree" title="使用注入器树来查找父组件。" target="_self"><span>浏览组件树</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/http" title="通过 HTTP 协议与远程服务器对话。" target="_self"><span>HttpClient</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/router" title="揭示如何通过 Angular 路由进行基本的屏幕导航。" target="_self"><span>路由与导航</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular 动画系统指南" aria-pressed="false"><span>动画</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/animations" title="Angular 动画的基础技术。" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/transition-and-triggers" title="转场与触发器的高级技术。" target="_self"><span>转场与触发器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/complex-animation-sequences" title="复杂的 Angular 动画序列。" target="_self"><span>复杂序列</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/reusable-animations" title="创建可复用的动画。" target="_self"><span>可复用动画</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/route-animations" title="为路由提供转场动画。" target="_self"><span>路由转场动画</span></a></div></aio-nav-item></div></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="把 Angular 用到你的实际工作中的一些技巧" aria-pressed="false"><span>其它技术</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/security" title="Angular 应用开发中的安全技术。" target="_self"><span>安全</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/i18n" title="把应用模板中的文本翻译成多种语言。" target="_self"><span>国际化（i18n）</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/accessibility" title="设计能被所有用户访问的应用" target="_self"><span>无障碍（a11y）</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular Service Worker: 控制应用资源的缓存。" aria-pressed="false"><span>Service Worker 与 PWA</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-intro" title="Angular 对 Service Worker 的实现提升了慢速或不稳定的网络连接下的用户体验。" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-getting-started" title="在 CLI 项目中启用 Service Worker，并在浏览器中查看效果。" target="_self"><span>快速上手</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/app-shell" title="在 CLI 项目中启用应用外壳。" target="_self"><span>应用外壳</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-communications" title="那些能让你和 Angular 的 Service Worker 通讯的服务类。" target="_self"><span>与 Service Worker 通讯</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-devops" title="使用 Service Worker 运行应用、管理应用更新、调试以及杀掉正在运行的应用。" target="_self"><span>生产环境下的 Service Worker</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-config" title="配置 Service Worker 的缓存行为。" target="_self"><span>Service Worker 配置</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/universal" title="使用 Angular Universal 在服务端渲染 HTML。" target="_self"><span>服务端渲染</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="把 AngularJS 应用增量式的升级到 Angular。" aria-pressed="false"><span>从 AngularJS 升级</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/upgrade" title="把 AngularJS 应用增量式的升级到 Angular。" target="_self"><span>升级步骤</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/upgrade-performance" title="用更灵活的方式把 AngularJS 升级到 Angular。" target="_self"><span>更关注性能的升级方式</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ajs-quick-reference" title="学习如何把 AngularJS 的概念映射到 Angular 中。" target="_self"><span>AngularJS 与 Angular 的概念对照</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="使用共享库扩展 Angular" aria-pressed="false"><span>开发 Angular 库</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/libraries" title="理解何时以及如何使用和创建库。" target="_self"><span>库概览</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/using-libraries" title="把已发布的库集成进你的应用中。" target="_self"><span>使用已发布的库</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/creating-libraries" title="通过创建、发布和使用你自己的库来扩展 Angular。" target="_self"><span>创建库</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="理解原理图" aria-pressed="false"><span>原理图（Schematic）</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/schematics" title="理解 Angular 如何使用原理图。" target="_self"><span>原理图概览</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/schematics-authoring" title="理解原理图的结构。" target="_self"><span>制作原理图</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/schematics-for-libraries" title="使用原理图来把你的库集成进 Angular CLI 中。" target="_self"><span>库的原理图</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/cli-builder" title="使用构建器定制 Angular CLI。" target="_self"><span>CLI 构建器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/web-worker" title="在 Angular CLI 中使用 Web Worker。" target="_self"><span>Web Worker</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="关于构建、测试和部署的信息" aria-pressed="false"><span>开发工作流</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="理解 AOT 预先编译器。" aria-pressed="false"><span>AOT 预先编译器</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/aot-compiler" title="学习为何以及如何使用预先编译器。" target="_self"><span>预先编译</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/angular-compiler-options" title="配置 AOT 编译。" target="_self"><span>Angular 编译器选项</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/aot-metadata-errors" title="解决 AOT 编译错误。" target="_self"><span>AOT 元数据错误</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/template-typecheck" title="Angular 中的模板类型检查。" target="_self"><span>模板类型检查</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/build" title="构建应用及为应用启动开发服务器。" target="_self"><span>构建与运行</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/bazel" title="如何配置你的环境，以利用 Bazel 进行构建和测试。" target="_self"><span>使用 Bazel 进行构建</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/testing" title="测试 Angular 应用的技巧与实践。" target="_self"><span>测试</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/deployment" title="了解如何部署 Angular 应用。" target="_self"><span>发布</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="与开发环境和工具集成起来" aria-pressed="false"><span>开发工具集成</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/language-service" title="使用 Angular 语言服务加速开发。" target="_self"><span>语言服务</span></a></div></aio-nav-item></div></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="工作空间与项目的结构，及其配置文件。" aria-pressed="false"><span>配置</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/file-structure" title="Angular 工作区在文件系统中是怎样的。" target="_self"><span>项目文件结构</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/workspace-config" title="&quot;angular.json&quot; 包含供 CLI 命令使用的工作区和项目默认配置。" target="_self"><span>工作区配置</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/npm-packages" title="开发期间和运行期间所需的 npm 包的说明。" target="_self"><span>npm 包</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/typescript-configuration" title="给 Angular 开发者的 TypeScript 配置。" target="_self"><span>TypeScript 配置</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/browser-support" title="浏览器支持与腻子脚本指南。" target="_self"><span>浏览器支持</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="Angular 的版本发布实践、更新与升级。" aria-pressed="false"><span>发布信息</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/updating" title="如何把 Angular 应用和库升级到最新版本。" target="_self"><span>保持最新</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/releases" title="Angular 的版本、发布、支持、弃用策略及实践。" target="_self"><span>发布实践</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/updating-to-version-9" title="支持把你的应用从 8 升级到 9。" target="_self"><span>升级到第 9 版</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/deprecations" title="弃用的 Angular API 和特性汇总。" target="_self"><span>弃用清单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/ivy" title="关于 Angular Ivy 的编译与渲染管道。" target="_self"><span>Angular Ivy</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="Angular 语法、编码、术语汇总。" aria-pressed="false"><span>快捷手册</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/cheatsheet" title="关于 Angular 常用编码技术的快速指南。" target="_self"><span>速查表</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/styleguide" title="写出 Angular 风格的程序" target="_self"><span>风格指南</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/glossary" title="Angular 中最重要的词汇的简要定义。" target="_self"><span>词汇表</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="Angular CLI 命令参考手册。" aria-pressed="false"><span>CLI 命令</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli" title="CLI 工具介绍、命令、语法" target="_self"><span>概览</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/usage-analytics-gathering" title="管理员如何从用户那里收集使用情况分析。" target="_self"><span>使用情况分析</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/add" title="ng add." target="_self"><span>ng add</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/analytics" title="ng analytics." target="_self"><span>ng analytics</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/build" title="ng build." target="_self"><span>ng build</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/config" title="ng config." target="_self"><span>ng config</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/deploy" title="ng deploy." target="_self"><span>ng deploy</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/doc" title="ng doc." target="_self"><span>ng doc</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/e2e" title="ng e2e." target="_self"><span>ng e2e</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/generate" title="ng generate." target="_self"><span>ng generate</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/help" title="ng help." target="_self"><span>ng help</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/lint" title="ng lint." target="_self"><span>ng lint</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/new" title="ng new." target="_self"><span>ng new</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/run" title="ng run." target="_self"><span>ng run</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/serve" title="ng serve." target="_self"><span>ng serve</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/test" title="ng test." target="_self"><span>ng test</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/update" title="ng update." target="_self"><span>ng update</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/version" title="ng version." target="_self"><span>ng version</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/xi18n" title="ng xi18n." target="_self"><span>ng xi18n</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="api" title="关于 Angular 中类和值的详细信息。" target="_self"><span>API 参考手册</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><div class="mat-divider ng-star-inserted" style="margin:4px 20px;border-top:1px solid #d3d3d3"></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://github.com/ng-docs/ng-docs.github.io/issues" title="github 上的中文互助问答区" target="_blank"><span>互助问答</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://material.angular.cn" title="Angular Material 组件库的中文文档" target="_blank"><span>官方 Material 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://ng.ant.design/" title="Ant Design 的 Angular 实现，服务于企业级后台产品。" target="_blank"><span>ng-zorro 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://devui.design/" title="“华为云” 开源的企业级组件库，核心设计思想是：致简、沉浸、灵活" target="_blank"><span>华为 DevUI 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://ng.mobile.ant.design/" title="Ant Design Mobile 的 Angular 实现，服务于无线产品。" target="_blank"><span>ng-zorro mobile 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://flutter.cn/" title="Flutter 中文文档站" target="_blank"><span>友站：Flutter 中文</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item></aio-nav-menu><div class="doc-version ng-tns-c18-1"><aio-select><div class="form-select-menu"><button class="form-select-button"><span><strong></strong></span><span>stable (v9.0.0)</span></button></div></aio-select></div></div></mat-sidenav><div class="cdk-visually-hidden cdk-focus-trap-anchor" aria-hidden="true"></div><mat-sidenav-content cdkscrollable="" class="mat-drawer-content mat-sidenav-content ng-star-inserted" style="margin-left:261px"><main role="main" class="sidenav-content" id="guide-lifecycle-hooks"><aio-mode-banner></aio-mode-banner><aio-doc-viewer class=""><div style="opacity:1"><div class="github-links"><a href="https://github.com/angular/angular-cn/edit/aio/aio/content/guide/lifecycle-hooks.md?message=docs%3A%20请简述你的修改..." aria-label="提供编辑建议" title="提供编辑建议"><i class="material-icons" aria-hidden="true" role="img">mode_edit</i></a></div><div class="content"><h1 id="lifecycle-hooks" translation-result="on">生命周期钩子<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#lifecycle-hooks"><i class="material-icons">link</i></a></h1><aio-toc class="embedded" ng-version="9.0.0-rc.11"><div class="toc-inner no-print collapsed ng-star-inserted"><button type="button" title="Expand/collapse contents" aria-label="Expand/collapse contents" class="toc-heading embedded secondary ng-star-inserted" aria-pressed="false">目录<mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon collapsed mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><ul class="toc-list embedded"><li class="h2 ng-star-inserted" title="组件生命周期钩子概览"><a href="guide/lifecycle-hooks#component-lifecycle-hooks-overview">组件生命周期钩子概览</a></li><li class="h2 ng-star-inserted" title="生命周期的顺序"><a href="guide/lifecycle-hooks#lifecycle-sequence">生命周期的顺序</a></li><li class="h2 ng-star-inserted" title="接口是可选的（严格来说）"><a href="guide/lifecycle-hooks#interfaces-are-optional-technically">接口是可选的（严格来说）</a></li><li class="h2 secondary ng-star-inserted" title="其它 Angular 生命周期钩子"><a href="guide/lifecycle-hooks#other-angular-lifecycle-hooks">其它 Angular 生命周期钩子</a></li><li class="h2 secondary ng-star-inserted" title="生命周期范例"><a href="guide/lifecycle-hooks#lifecycle-examples">生命周期范例</a></li><li class="h2 secondary ng-star-inserted" title="Peek-a-boo：全部钩子"><a href="guide/lifecycle-hooks#peek-a-boo-all-hooks">Peek-a-boo：全部钩子</a></li><li class="h2 secondary ng-star-inserted" title="窥探 OnInit 和 OnDestroy"><a href="guide/lifecycle-hooks#spying-oninit-and-ondestroy">窥探 <em>OnInit</em> 和 <em>OnDestroy</em></a></li><li class="h3 secondary ng-star-inserted" title="OnInit()"><a href="guide/lifecycle-hooks#oninit"><em>OnInit()</em></a></li><li class="h3 secondary ng-star-inserted" title="OnDestroy()"><a href="guide/lifecycle-hooks#ondestroy"><em>OnDestroy()</em></a></li><li class="h2 secondary ng-star-inserted" title="OnChanges()"><a href="guide/lifecycle-hooks#onchanges"><em>OnChanges()</em></a></li><li class="h2 secondary ng-star-inserted" title="DoCheck()"><a href="guide/lifecycle-hooks#docheck"><em>DoCheck()</em></a></li><li class="h2 secondary ng-star-inserted" title="AfterView"><a href="guide/lifecycle-hooks#afterview">AfterView</a></li><li class="h3 secondary ng-star-inserted" title="遵循单向数据流规则"><a href="guide/lifecycle-hooks#abide-by-the-unidirectional-data-flow-rule">遵循单向数据流规则</a></li><li class="h2 secondary ng-star-inserted" title="AfterContent"><a href="guide/lifecycle-hooks#aftercontent">AfterContent</a></li><li class="h3 secondary ng-star-inserted" title="内容投影"><a href="guide/lifecycle-hooks#content-projection">内容投影</a></li><li class="h3 secondary ng-star-inserted" title="AfterContent 钩子"><a href="guide/lifecycle-hooks#aftercontent-hooks">AfterContent 钩子</a></li><li class="h3 secondary ng-star-inserted" title="使用 AfterContent 时，无需担心单向数据流规则"><a href="guide/lifecycle-hooks#no-unidirectional-flow-worries-with-aftercontent">使用 <strong>AfterContent</strong> 时，无需担心单向数据流规则</a></li></ul><button type="button" title="Expand/collapse contents" aria-label="Expand/collapse contents" class="toc-more-items embedded material-icons collapsed ng-star-inserted" aria-pressed="false"></button></div></aio-toc><h1 translation-origin="off" id="lifecycle-hooks">Lifecycle hooks<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#lifecycle-hooks"><i class="material-icons">link</i></a></h1><p translation-result="on">每个组件都有一个被 Angular 管理的生命周期。</p><p translation-origin="off">A component has a lifecycle managed by Angular.</p><p translation-result="on">Angular 创建和渲染组件及其子组件，当它们绑定的属性发生变化时检查它们，并在从 DOM 中移除它之前销毁它们。</p><p translation-origin="off">Angular creates and renders components along with their children, checks when their data-bound properties change, and destroys them before removing them from the DOM.</p><p translation-result="on">Angular 提供了<strong>生命周期钩子</strong>，把它们生命中的这些关键时刻暴露出来，赋予你在它们发生时采取行动的能力。</p><p translation-origin="off">Angular offers <strong>lifecycle hooks</strong> that provide visibility into these key life moments and the ability to act when they occur.</p><p translation-result="on">除了那些组件内容和视图相关的钩子外,指令有相同生命周期钩子。</p><p translation-origin="off">A directive has the same set of lifecycle hooks.</p><a id="hooks-overview"></a><h2 id="component-lifecycle-hooks-overview" translation-result="on">组件生命周期钩子概览<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#component-lifecycle-hooks-overview"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="component-lifecycle-hooks-overview">Component lifecycle hooks overview<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#component-lifecycle-hooks-overview"><i class="material-icons">link</i></a></h2><p translation-result="on">指令和组件的实例有一个生命周期：当 Angular 新建、更新和销毁它们时触发。 通过实现一个或多个 Angular <code>core</code> 库里定义的<em>生命周期钩子</em>接口，开发者可以介入该生命周期中的这些关键时刻。</p><p translation-origin="off">Directive and component instances have a lifecycle as Angular creates, updates, and destroys them. Developers can tap into key moments in that lifecycle by implementing one or more of the <em>lifecycle hook</em> interfaces in the Angular <code>core</code> library.</p><p translation-result="on">每个接口都有唯一的一个钩子方法，它们的名字是由接口名再加上 <code>ng</code> 前缀构成的。比如，<code><a href="api/core/OnInit" class="code-anchor">OnInit</a></code> 接口的钩子方法叫做 <code>ngOnInit</code>， Angular 在创建组件后立刻调用它，：</p><p translation-origin="off">Each interface has a single hook method whose name is the interface name prefixed with <code>ng</code>. For example, the <code><a href="api/core/OnInit" class="code-anchor">OnInit</a></code> interface has a hook method named <code>ngOnInit()</code> that Angular calls shortly after creating the component:</p><code-example path="lifecycle-hooks/src/app/peek-a-boo.component.ts" region="ngOnInit" header="peek-a-boo.component.ts (excerpt)" ng-version="9.0.0-rc.11"><div style="display:none">export class PeekABoo implements <a href="api/core/OnInit" class="code-anchor">OnInit</a> { constructor(private logger: LoggerService) { } // implement <a href="api/core/OnInit" class="code-anchor">OnInit</a>'s `ngOnInit` method ngOnInit() { this.logIt(`<a href="api/core/OnInit" class="code-anchor">OnInit</a>`); } logIt(msg: string) { this.logger.log(`#${nextId++} ${msg}`); } }</div><header class="ng-star-inserted">peek-a-boo.component.ts (excerpt)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from peek-a-boo.component.ts (excerpt)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">PeekABoo</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">constructor</span><span class="pun">(</span><span class="kwd">private</span><span class="pln"> logger</span><span class="pun">:</span><span class="pln"> </span><span class="typ">LoggerService</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

  </span><span class="com">// implement </span><a href="api/core/OnInit" class="code-anchor"><span class="com">OnInit</span></a><span class="com">'s `ngOnInit` method</span><span class="pln">
  ngOnInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logIt</span><span class="pun">(</span><span class="str">`</span><a href="api/core/OnInit" class="code-anchor"><span class="str">OnInit</span></a><span class="str">`</span><span class="pun">);</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

  logIt</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logger</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">`#${nextId++} ${msg}`</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">没有指令或者组件会实现所有这些接口，并且有些钩子只对组件有意义。只有在指令/组件中<em>定义过的</em>那些钩子方法才会被 Angular 调用。</p><p translation-origin="off">No directive or component will implement all of the lifecycle hooks. Angular only calls a directive/component hook method <em>if it is defined</em>.</p><a id="hooks-purpose-timing"></a><h2 id="lifecycle-sequence" translation-result="on">生命周期的顺序<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#lifecycle-sequence"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="lifecycle-sequence">Lifecycle sequence<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#lifecycle-sequence"><i class="material-icons">link</i></a></h2><p translation-result="on">当 Angular 使用构造函数新建一个组件或指令后，就会按下面的顺序在特定时刻调用这些生命周期钩子方法：</p><p translation-origin="off"><em>After</em> creating a component/directive by calling its constructor, Angular calls the lifecycle hook methods in the following sequence at specific moments:</p><table width="100%"><colgroup><col width="20%"><col width="80%"></colgroup><tbody><tr><th><p translation-result="on">钩子</p><p translation-origin="off">Hook</p></th><th><p translation-result="on">用途及时机</p><p translation-origin="off">Purpose and Timing</p></th></tr><tr style="vertical-align:top"><td><p> <code>ngOnChanges()</code></p></td><td><p translation-result="on">当 Angular（重新）设置数据绑定输入属性时响应。 该方法接受当前和上一属性值的 <code><a href="api/core/SimpleChanges" class="code-anchor">SimpleChanges</a></code> 对象</p><p translation-origin="off">Respond when Angular (re)sets data-bound input properties. The method receives a <code><a href="api/core/SimpleChanges" class="code-anchor">SimpleChanges</a></code> object of current and previous property values.</p><p translation-result="on">在 <code>ngOnInit()</code> 之前以及所绑定的一个或多个输入属性的值发生变化时都会调用。</p><p translation-origin="off">Called before <code>ngOnInit()</code> and whenever one or more data-bound input properties change.</p></td></tr><tr style="vertical-align:top"><td><p> <code>ngOnInit()</code></p></td><td><p translation-result="on">在 Angular 第一次显示数据绑定和设置指令/组件的输入属性之后，初始化指令/组件。</p><p translation-origin="off">Initialize the directive/component after Angular first displays the data-bound properties and sets the directive/component's input properties.</p><p translation-result="on">在第一轮 <code>ngOnChanges()</code> 完成之后调用，只调用<strong>一次</strong>。</p><p translation-origin="off">Called <em>once</em>, after the <em>first</em> <code>ngOnChanges()</code>.</p></td></tr><tr style="vertical-align:top"><td><p> <code>ngDoCheck()</code></p></td><td><p translation-result="on">检测，并在发生 Angular 无法或不愿意自己检测的变化时作出反应。</p><p translation-origin="off">Detect and act upon changes that Angular can't or won't detect on its own.</p><p translation-result="on">在每个变更检测周期中，紧跟在 <code>ngOnChanges()</code> 和 <code>ngOnInit()</code> 后面调用。</p><p translation-origin="off">Called during every change detection run, immediately after <code>ngOnChanges()</code> and <code>ngOnInit()</code>.</p></td></tr><tr style="vertical-align:top"><td><p> <code><a href="api/router/RouterLinkActive#ngAfterContentInit" class="code-anchor">ngAfterContentInit()</a></code></p></td><td><p translation-result="on">当 Angular 把外部内容投影进组件/指令的视图之后调用。</p><p translation-origin="off">Respond after Angular projects external content into the component's view / the view that a directive is in.</p><p translation-result="on">第一次 <code>ngDoCheck()</code> 之后调用，只调用一次。</p><p translation-origin="off">Called <em>once</em> after the first <code>ngDoCheck()</code>.</p></td></tr><tr style="vertical-align:top"><td><p> <code>ngAfterContentChecked()</code></p></td><td><p translation-result="on">每当 Angular 完成被投影组件内容的变更检测之后调用。</p><p translation-origin="off">Respond after Angular checks the content projected into the directive/component.</p><p translation-result="on"> <code><a href="api/router/RouterLinkActive#ngAfterContentInit" class="code-anchor">ngAfterContentInit()</a></code> 和每次 <code>ngDoCheck()</code> 之后调用</p><p translation-origin="off">Called after the <code><a href="api/router/RouterLinkActive#ngAfterContentInit" class="code-anchor">ngAfterContentInit()</a></code> and every subsequent <code>ngDoCheck()</code>.</p></td></tr><tr style="vertical-align:top"><td><p> <code><a href="api/forms/NgForm#ngAfterViewInit" class="code-anchor">ngAfterViewInit()</a></code></p></td><td><p translation-result="on">当 Angular 初始化完组件视图及其子视图之后调用。</p><p translation-origin="off">Respond after Angular initializes the component's views and child views / the view that a directive is in.</p><p translation-result="on">第一次 <code>ngAfterContentChecked()</code> 之后调用，只调用一次。</p><p translation-origin="off">Called <em>once</em> after the first <code>ngAfterContentChecked()</code>.</p></td></tr><tr style="vertical-align:top"><td><p> <code>ngAfterViewChecked()</code></p></td><td><p translation-result="on">每当 Angular 做完组件视图和子视图的变更检测之后调用。</p><p translation-origin="off">Respond after Angular checks the component's views and child views / the view that a directive is in.</p><p translation-result="on"> <code><a href="api/forms/NgForm#ngAfterViewInit" class="code-anchor">ngAfterViewInit()</a></code> 和每次 <code>ngAfterContentChecked()</code> 之后调用。</p><p translation-origin="off">Called after the <code><a href="api/forms/NgForm#ngAfterViewInit" class="code-anchor">ngAfterViewInit()</a></code> and every subsequent <code>ngAfterContentChecked()</code>.</p></td></tr><tr style="vertical-align:top"><td><p> <code>ngOnDestroy()</code></p></td><td><p translation-result="on">每当 Angular 每次销毁指令/组件之前调用并清扫。 在这儿反订阅可观察对象和分离事件处理器，以防内存泄漏。</p><p translation-origin="off">Cleanup just before Angular destroys the directive/component. Unsubscribe Observables and detach event handlers to avoid memory leaks.</p><p translation-result="on">在 Angular 销毁指令/组件之前调用。</p><p translation-origin="off">Called <em>just before</em> Angular destroys the directive/component.</p></td></tr></tbody></table><a id="interface-optional"></a><h2 id="interfaces-are-optional-technically" translation-result="on">接口是可选的（严格来说）<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#interfaces-are-optional-technically"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="interfaces-are-optional-technically">Interfaces are optional (technically)<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#interfaces-are-optional-technically"><i class="material-icons">link</i></a></h2><p translation-result="on">从纯技术的角度讲，接口对 JavaScript 和 TypeScript 的开发者都是可选的。JavaScript 语言本身没有接口。 Angular 在运行时看不到 TypeScript 接口，因为它们在编译为 JavaScript 的时候已经消失了。</p><p translation-origin="off">The interfaces are optional for JavaScript and Typescript developers from a purely technical perspective. The JavaScript language doesn't have interfaces. Angular can't see TypeScript interfaces at runtime because they disappear from the transpiled JavaScript.</p><p translation-result="on">幸运的是，它们并不是必须的。 你并不需要在指令和组件上添加生命周期钩子接口就能获得钩子带来的好处。</p><p translation-origin="off">Fortunately, they aren't necessary. You don't have to add the lifecycle hook interfaces to directives and components to benefit from the hooks themselves.</p><p translation-result="on">Angular 会去检测这些指令和组件的类，一旦发现钩子方法被定义了，就调用它们。 Angular 会找到并调用像 <code>ngOnInit()</code> 这样的钩子方法，有没有接口无所谓。</p><p translation-origin="off">Angular instead inspects directive and component classes and calls the hook methods <em>if they are defined</em>. Angular finds and calls methods like <code>ngOnInit()</code>, with or without the interfaces.</p><p translation-result="on">虽然如此，在 TypeScript 指令类中添加接口是一项最佳实践，它可以获得强类型和 IDE 等编辑器带来的好处。</p><p translation-origin="off">Nonetheless, it's good practice to add interfaces to TypeScript directive classes in order to benefit from strong typing and editor tooling.</p><a id="other-lifecycle-hooks"></a><h2 id="other-angular-lifecycle-hooks" translation-result="on">其它 Angular 生命周期钩子<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#other-angular-lifecycle-hooks"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="other-angular-lifecycle-hooks">Other Angular lifecycle hooks<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#other-angular-lifecycle-hooks"><i class="material-icons">link</i></a></h2><p translation-result="on">Angular 的其它子系统除了有这些组件钩子外，还可能有它们自己的生命周期钩子。</p><p translation-origin="off">Other Angular sub-systems may have their own lifecycle hooks apart from these component hooks.</p><p translation-result="on">第三方库也可能会实现它们自己的钩子，以便让这些开发者在使用时能做更多的控制。</p><p translation-origin="off">3rd party libraries might implement their hooks as well in order to give developers more control over how these libraries are used.</p><a id="the-sample"></a><h2 id="lifecycle-examples" translation-result="on">生命周期范例<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#lifecycle-examples"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="lifecycle-examples">Lifecycle examples<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#lifecycle-examples"><i class="material-icons">link</i></a></h2><p translation-result="on"><live-example ng-version="9.0.0-rc.11"><span style="display:none"></span><span><span class="ng-star-inserted"><a target="_blank" title="现场演练" href="generated/live-examples/lifecycle-hooks/stackblitz.html">现场演练</a><span class="ng-star-inserted"> / <a download="" title="下载范例" href="generated/zips/lifecycle-hooks/lifecycle-hooks.zip">下载范例</a></span></span></span></live-example>通过在受控于根组件 <code>AppComponent</code> 的一些组件上进行的一系列练习，演示了生命周期钩子的运作方式。</p><p translation-origin="off">The<live-example ng-version="9.0.0-rc.11"><span style="display:none"></span><span><span class="ng-star-inserted"><a target="_blank" title="现场演练" href="generated/live-examples/lifecycle-hooks/stackblitz.html">现场演练</a><span class="ng-star-inserted"> / <a download="" title="下载范例" href="generated/zips/lifecycle-hooks/lifecycle-hooks.zip">下载范例</a></span></span></span></live-example>demonstrates the lifecycle hooks in action through a series of exercises presented as components under the control of the root <code>AppComponent</code>.</p><p translation-result="on">它们遵循了一个常用的模式：用<em>子组件</em>演示一个或多个生命周期钩子方法，而<em>父组件</em>被当作该<em>子组件</em>的测试台。</p><p translation-origin="off">They follow a common pattern: a <em>parent</em> component serves as a test rig for a <em>child</em> component that illustrates one or more of the lifecycle hook methods.</p><p translation-result="on">下面是每个练习简短的描述：</p><p translation-origin="off">Here's a brief description of each exercise:</p><table width="100%"><colgroup><col width="20%"><col width="80%"></colgroup><tbody><tr><th><p translation-result="on">组件</p><p translation-origin="off">Component</p></th><th><p translation-result="on">说明</p><p translation-origin="off">Description</p></th></tr><tr style="vertical-align:top"><td><p><a href="guide/lifecycle-hooks#peek-a-boo">Peek-a-boo</a></p></td><td><p translation-result="on">展示每个生命周期钩子，每个钩子方法都会在屏幕上显示一条日志。</p><p translation-origin="off">Demonstrates every lifecycle hook. Each hook method writes to the on-screen log.</p></td></tr><tr style="vertical-align:top"><td><p><a href="guide/lifecycle-hooks#spy">Spy</a></p></td><td><p translation-result="on">指令也同样有生命周期钩子。<code>SpyDirective</code> 可以利用 <code>ngOnInit</code> 和 <code>ngOnDestroy</code> 钩子在它所监视的每个元素被创建或销毁时输出日志。</p><p translation-origin="off">Directives have lifecycle hooks too. A <code>SpyDirective</code> can log when the element it spies upon is created or destroyed using the <code>ngOnInit</code> and <code>ngOnDestroy</code> hooks.</p><p translation-result="on">本例把 <code>SpyDirective</code> 应用到父组件里的 <code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code><em>英雄</em>重复器(repeater)的 <code>&lt;div&gt;</code> 里面。</p><p translation-origin="off">This example applies the <code>SpyDirective</code> to a <code>&lt;div&gt;</code> in an <code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> <em>hero</em> repeater managed by the parent <code>SpyComponent</code>.</p></td></tr><tr style="vertical-align:top"><td><p><a href="guide/lifecycle-hooks#onchanges">OnChanges</a></p></td><td><p translation-result="on">这里将会看到：每当组件的输入属性发生变化时，Angular 会如何以 <code><a href="api/core/QueryList#changes" class="code-anchor">changes</a></code> 对象作为参数去调用 <code>ngOnChanges()</code> 钩子。 展示了该如何理解和使用 <code><a href="api/core/QueryList#changes" class="code-anchor">changes</a></code> 对象。</p><p translation-origin="off">See how Angular calls the <code>ngOnChanges()</code> hook with a <code><a href="api/core/QueryList#changes" class="code-anchor">changes</a></code> object every time one of the component input properties changes. Shows how to interpret the <code><a href="api/core/QueryList#changes" class="code-anchor">changes</a></code> object.</p></td></tr><tr style="vertical-align:top"><td><p><a href="guide/lifecycle-hooks#docheck">DoCheck</a></p></td><td><p translation-result="on">实现了一个 <code>ngDoCheck()</code> 方法，通过它可以自定义变更检测逻辑。 这里将会看到：Angular 会用什么频度调用这个钩子，监视它的变化，并把这些变化输出成一条日志。</p><p translation-origin="off">Implements an <code>ngDoCheck()</code> method with custom change detection. See how often Angular calls this hook and watch it post changes to a log.</p></td></tr><tr style="vertical-align:top"><td><p><a href="guide/lifecycle-hooks#afterview">AfterView</a></p></td><td><p translation-result="on">显示 Angular 中的<em>视图</em>所指的是什么。 演示了 <code>ngAfterViewInit</code> 和 <code>ngAfterViewChecked</code> 钩子。</p><p translation-origin="off">Shows what Angular means by a <em>view</em>. Demonstrates the <code>ngAfterViewInit</code> and <code>ngAfterViewChecked</code> hooks.</p></td></tr><tr style="vertical-align:top"><td><p><a href="guide/lifecycle-hooks#aftercontent">AfterContent</a></p></td><td><p translation-result="on">展示如何把外部内容投影进组件中，以及如何区分“投影进来的内容”和“组件的子视图”。 演示了 <code>ngAfterContentInit</code> 和 <code>ngAfterContentChecked</code> 钩子。</p><p translation-origin="off">Shows how to project external content into a component and how to distinguish projected content from a component's view children. Demonstrates the <code>ngAfterContentInit</code> and <code>ngAfterContentChecked</code> hooks.</p></td></tr><tr style="vertical-align:top"><td><p translation-result="on">计数器</p><p translation-origin="off">Counter</p></td><td><p translation-result="on">演示了组件和指令的组合，它们各自有自己的钩子。</p><p translation-origin="off">Demonstrates a combination of a component and a directive each with its own hooks.</p><p translation-result="on">在这个例子中，每当父组件递增它的输入属性 <code>counter</code> 时，<code>CounterComponent</code> 就会通过 <code>ngOnChanges</code> 记录一条变更。 同时，前一个例子中的 <code>SpyDirective</code> 被用于在 <code>CounterComponent</code> 上提供日志，它可以同时观察到日志的创建和销毁过程。</p><p translation-origin="off">In this example, a <code>CounterComponent</code> logs a change (via <code>ngOnChanges</code>) every time the parent component increments its input counter property. Meanwhile, the <code>SpyDirective</code> from the previous example is applied to the <code>CounterComponent</code> log where it watches log entries being created and destroyed.</p></td></tr></tbody></table><p translation-result="on">本文剩下的部分将详细讨论这些练习。</p><p translation-origin="off">The remainder of this page discusses selected exercises in further detail.</p><a id="peek-a-boo"></a><h2 id="peek-a-boo-all-hooks" translation-result="on">Peek-a-boo：全部钩子<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#peek-a-boo-all-hooks"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="peek-a-boo-all-hooks">Peek-a-boo: all hooks<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#peek-a-boo-all-hooks"><i class="material-icons">link</i></a></h2><p translation-result="on"><code>PeekABooComponent</code> 组件演示了组件中所有可能存在的钩子。</p><p translation-origin="off">The <code>PeekABooComponent</code> demonstrates all of the hooks in one component.</p><p translation-result="on">你可能很少、或者永远不会像这里一样实现所有这些接口。 之所以在 peek-a-boo 中这么做，是为了演示 Angular 是如何按照期望的顺序调用这些钩子的。</p><p translation-origin="off">You would rarely, if ever, implement all of the interfaces like this. The peek-a-boo exists to show how Angular calls the hooks in the expected order.</p><p translation-result="on">用户点击<strong>Create...</strong>按钮，然后点击<strong>Destroy...</strong>按钮后，日志的状态如下图所示：</p><p translation-origin="off">This snapshot reflects the state of the log after the user clicked the <em>Create...</em> button and then the <em>Destroy...</em> button.</p><div class="lightbox"><img src="generated/images/guide/lifecycle-hooks/peek-a-boo.png" alt="Peek-a-boo" width="309" height="366"></div><p translation-result="on">日志信息的日志和所规定的钩子调用顺序是一致的： <code><a href="api/core/OnChanges" class="code-anchor">OnChanges</a></code>、<code><a href="api/core/OnInit" class="code-anchor">OnInit</a></code>、<code><a href="api/core/DoCheck" class="code-anchor">DoCheck</a></code>&nbsp;(3x)、<code><a href="api/core/AfterContentInit" class="code-anchor">AfterContentInit</a></code>、<code><a href="api/core/AfterContentChecked" class="code-anchor">AfterContentChecked</a></code>&nbsp;(3x)、 <code><a href="api/core/AfterViewInit" class="code-anchor">AfterViewInit</a></code>、<code><a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a></code>&nbsp;(3x)和 <code><a href="api/core/OnDestroy" class="code-anchor">OnDestroy</a></code></p><p translation-origin="off">The sequence of log messages follows the prescribed hook calling order: <code><a href="api/core/OnChanges" class="code-anchor">OnChanges</a></code>, <code><a href="api/core/OnInit" class="code-anchor">OnInit</a></code>, <code><a href="api/core/DoCheck" class="code-anchor">DoCheck</a></code>&nbsp;(3x), <code><a href="api/core/AfterContentInit" class="code-anchor">AfterContentInit</a></code>, <code><a href="api/core/AfterContentChecked" class="code-anchor">AfterContentChecked</a></code>&nbsp;(3x), <code><a href="api/core/AfterViewInit" class="code-anchor">AfterViewInit</a></code>, <code><a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a></code>&nbsp;(3x), and <code><a href="api/core/OnDestroy" class="code-anchor">OnDestroy</a></code>.</p><div class="alert is-helpful"><p translation-result="on">构造函数本质上不应该算作 Angular 的钩子。 记录确认了在创建期间那些输入属性(这里是 <code>name</code> 属性)没有被赋值。</p><p translation-origin="off">The constructor isn't an Angular hook <em>per se</em>. The log confirms that input properties (the <code>name</code> property in this case) have no assigned values at construction.</p></div><p translation-result="on">如果用户点击<em>Update Hero</em>按钮，就会看到另一个 <code><a href="api/core/OnChanges" class="code-anchor">OnChanges</a></code> 和至少两组 <code><a href="api/core/DoCheck" class="code-anchor">DoCheck</a></code>、<code><a href="api/core/AfterContentChecked" class="code-anchor">AfterContentChecked</a></code> 和 <code><a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a></code> 钩子。 显然，这三种钩子被触发了<em>很多次</em>，必须让这三种钩子里的逻辑尽可能的精简！</p><p translation-origin="off">Had the user clicked the <em>Update Hero</em> button, the log would show another <code><a href="api/core/OnChanges" class="code-anchor">OnChanges</a></code> and two more triplets of <code><a href="api/core/DoCheck" class="code-anchor">DoCheck</a></code>, <code><a href="api/core/AfterContentChecked" class="code-anchor">AfterContentChecked</a></code> and <code><a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a></code>. Clearly these three hooks fire <em>often</em>. Keep the logic in these hooks as lean as possible!</p><p translation-result="on">下一个例子就聚焦于这些钩子的细节上。</p><p translation-origin="off">The next examples focus on hook details.</p><a id="spy"></a><h2 id="spying-oninit-and-ondestroy" translation-result="on">窥探 <em>OnInit</em> 和 <em>OnDestroy</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#spying-oninit-and-ondestroy"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="spying-oninit-and-ondestroy">Spying <em>OnInit</em> and <em>OnDestroy</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#spying-oninit-and-ondestroy"><i class="material-icons">link</i></a></h2><p translation-result="on">潜入这两个 spy 钩子来发现一个元素是什么时候被初始化或者销毁的。</p><p translation-origin="off">Go undercover with these two spy hooks to discover when an element is initialized or destroyed.</p><p translation-result="on">指令是一种完美的渗透方式，这些英雄们永远不会知道该指令的存在。</p><p translation-origin="off">This is the perfect infiltration job for a directive. The heroes will never know they're being watched.</p><div class="alert is-helpful"><p translation-result="on">不开玩笑了，注意下面两个关键点：</p><p translation-origin="off">Kidding aside, pay attention to two key points:</p><ol><li><p translation-result="on">就像对组件一样，Angular 也会对<em>指令</em>调用这些钩子方法。<br><br></p><p translation-origin="off">Angular calls hook methods for <em>directives</em> as well as components.<br><br></p></li><li><p translation-result="on">一个侦探(spy)指令可以让你在无法直接修改 DOM 对象实现代码的情况下，透视其内部细节。 显然，你不能修改一个原生 <code>&lt;div&gt;</code> 元素的实现代码。 你同样不能修改第三方组件。 但你用一个指令就能监视它们了。</p><p translation-origin="off">A spy directive can provide insight into a DOM object that you cannot change directly. Obviously you can't touch the implementation of a native <code>&lt;div&gt;</code>. You can't modify a third party component either. But you can watch both with a directive.</p></li></ol></div><p translation-result="on">这个鬼鬼祟祟的侦探指令很简单，几乎完全由 <code>ngOnInit()</code> 和 <code>ngOnDestroy()</code> 钩子组成，它通过一个注入进来的 <code>LoggerService</code> 把消息记录到父组件中去。</p><p translation-origin="off">The sneaky spy directive is simple, consisting almost entirely of <code>ngOnInit()</code> and <code>ngOnDestroy()</code> hooks that log messages to the parent via an injected <code>LoggerService</code>.</p><code-example path="lifecycle-hooks/src/app/spy.directive.ts" region="spy-directive" header="src/app/spy.directive.ts" ng-version="9.0.0-rc.11"><div style="display:none">// Spy on any element to which it is applied. // Usage: &lt;div mySpy&gt;...&lt;/div&gt; @<a href="api/core/Directive" class="code-anchor">Directive</a>({selector: '[mySpy]'}) export class SpyDirective implements <a href="api/core/OnInit" class="code-anchor">OnInit</a>, <a href="api/core/OnDestroy" class="code-anchor">OnDestroy</a> { constructor(private logger: LoggerService) { } ngOnInit() { this.logIt(`<a href="" class="code-anchor">onInit</a>`); } ngOnDestroy() { this.logIt(`onDestroy`); } private logIt(msg: string) { this.logger.log(`Spy #${nextId++} ${msg}`); } }</div><header class="ng-star-inserted">src/app/spy.directive.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/spy.directive.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">// Spy on any element to which it is applied.</span><span class="pln">
</span><span class="com">// Usage: &lt;div mySpy&gt;...&lt;/div&gt;</span><span class="pln">
</span><span class="lit">@</span><a href="api/core/Directive" class="code-anchor"><span class="lit">Directive</span></a><span class="pun">({</span><span class="pln">selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'[mySpy]'</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">SpyDirective</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/OnInit" class="code-anchor"><span class="typ">OnInit</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/OnDestroy" class="code-anchor"><span class="typ">OnDestroy</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">

  </span><span class="kwd">constructor</span><span class="pun">(</span><span class="kwd">private</span><span class="pln"> logger</span><span class="pun">:</span><span class="pln"> </span><span class="typ">LoggerService</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

  ngOnInit</span><span class="pun">()</span><span class="pln">    </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logIt</span><span class="pun">(</span><span class="str">`</span><a href="" class="code-anchor"><span class="str">onInit</span></a><span class="str">`</span><span class="pun">);</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

  ngOnDestroy</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logIt</span><span class="pun">(</span><span class="str">`onDestroy`</span><span class="pun">);</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

  </span><span class="kwd">private</span><span class="pln"> logIt</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logger</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">`Spy #${nextId++} ${msg}`</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">你可以把这个侦探指令写到任何原生元素或组件元素上，它将与所在的组件同时初始化和销毁。 下面是把它附加到用来重复显示英雄数据的这个 <code>&lt;div&gt;</code> 上。</p><p translation-origin="off">You can apply the spy to any native or component element and it'll be initialized and destroyed at the same time as that element. Here it is attached to the repeated hero <code>&lt;div&gt;</code>:</p><code-example path="lifecycle-hooks/src/app/spy.component.html" region="template" header="src/app/spy.component.html" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes" mySpy class="heroes"&gt; {{hero}} &lt;/div&gt;</div><header class="ng-star-inserted">src/app/spy.component.html</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/spy.component.html">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div</span><span class="pln"> *</span><a href="api/common/NgForOf" class="code-anchor"><span class="atn">ngFor</span></a><span class="pun">=</span><span class="atv">"let hero of heroes"</span><span class="pln"> </span><span class="atn">mySpy</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"heroes"</span><span class="tag">&gt;</span><span class="pln">
  {{hero}}
</span><span class="tag">&lt;/div&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">每个“侦探”的出生和死亡也同时标记出了存放英雄的那个 <code>&lt;div&gt;</code> 的出生和死亡。<em>钩子记录</em>中的结构是这样的：</p><p translation-origin="off">Each spy's birth and death marks the birth and death of the attached hero <code>&lt;div&gt;</code> with an entry in the <em>Hook Log</em> as seen here:</p><div class="lightbox"><img src="generated/images/guide/lifecycle-hooks/spy-directive.gif" alt="Spy Directive" width="656" height="378"></div><p translation-result="on">添加一个英雄就会产生一个新的英雄 <code>&lt;div&gt;</code>。侦探的 <code>ngOnInit()</code> 记录下了这个事件。</p><p translation-origin="off">Adding a hero results in a new hero <code>&lt;div&gt;</code>. The spy's <code>ngOnInit()</code> logs that event.</p><p translation-result="on"><em>Reset</em> 按钮清除了这个 <code>heroes</code> 列表。 Angular 从 DOM 中移除了所有英雄的 div，并且同时销毁了附加在这些 div 上的侦探指令。 侦探的 <code>ngOnDestroy()</code> 方法汇报了它自己的临终时刻。</p><p translation-origin="off">The <em>Reset</em> button clears the <code>heroes</code> list. Angular removes all hero <code>&lt;div&gt;</code> elements from the DOM and destroys their spy directives at the same time. The spy's <code>ngOnDestroy()</code> method reports its last moments.</p><p translation-result="on">在真实的应用程序中，<code>ngOnInit()</code> 和 <code>ngOnDestroy()</code> 方法扮演着更重要的角色。</p><p translation-origin="off">The <code>ngOnInit()</code> and <code>ngOnDestroy()</code> methods have more vital roles to play in real applications.</p><a id="oninit"></a><h3 id="oninit"><em>OnInit()</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#oninit"><i class="material-icons">link</i></a></h3><p translation-result="on">使用 <code>ngOnInit()</code> 有两个原因：</p><p translation-origin="off">Use <code>ngOnInit()</code> for two main reasons:</p><ol><li><p translation-result="on">在构造函数之后马上执行复杂的初始化逻辑</p><p translation-origin="off">To perform complex initializations shortly after construction.</p></li><li><p translation-result="on">在 Angular 设置完输入属性之后，对该组件进行准备。</p><p translation-origin="off">To set up the component after Angular sets the input properties.</p></li></ol><p translation-result="on">有经验的开发者会认同组件应该能很便宜和安全的构造出来。</p><p translation-origin="off">Experienced developers agree that components should be cheap and safe to construct.</p><div class="alert is-helpful"><p translation-result="on">Misko Hevery，Angular 项目的组长，在<a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/">这里解释</a>了你为什么应该避免复杂的构造函数逻辑。</p><p translation-origin="off">Misko Hevery, Angular team lead, <a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/">explains why</a> you should avoid complex constructor logic.</p></div><p translation-result="on">不要在组件的构造函数中获取数据？ 在测试环境下新建组件时或在你决定要显示它之前，不应该担心它会尝试联系远程服务器。 构造函数中除了使用简单的值对局部变量进行初始化之外，什么都不应该做。</p><p translation-origin="off">Don't fetch data in a component constructor. You shouldn't worry that a new component will try to contact a remote server when created under test or before you decide to display it. Constructors should do no more than set the initial local variables to simple values.</p><p translation-result="on"><code>ngOnInit()</code> 是组件获取初始数据的好地方。<a href="tutorial/toh-pt4#oninit">指南</a>中讲解了如何这样做。</p><p translation-origin="off">An <code>ngOnInit()</code> is a good place for a component to fetch its initial data. The <a href="tutorial/toh-pt4#oninit">Tour of Heroes Tutorial</a> guide shows how.</p><p translation-result="on">另外还要记住，在指令的<em>构造函数完成之前</em>，那些被绑定的输入属性还都没有值。 如果你需要基于这些属性的值来初始化这个指令，这种情况就会出问题。 而当 <code>ngOnInit()</code> 执行的时候，这些属性都已经被正确的赋值过了。</p><p translation-origin="off">Remember also that a directive's data-bound input properties are not set until <em>after construction</em>. That's a problem if you need to initialize the directive based on those properties. They'll have been set when <code>ngOnInit()</code> runs.</p><div class="alert is-helpful"><p translation-result="on"> <code>ngOnChanges()</code> 方法是你访问这些属性的第一次机会。Angular 会在 <code>ngOnInit()</code> 之前调用 <code>ngOnChanges()</code>，之后还会调用很多次。但只会调用一次 <code>ngOnInit()</code>。</p><p translation-origin="off">The <code>ngOnChanges()</code> method is your first opportunity to access those properties. Angular calls <code>ngOnChanges()</code> before <code>ngOnInit()</code> and many times after that. It only calls <code>ngOnInit()</code> once.</p></div><p translation-result="on">你可以信任 Angular 会在创建组件后立刻调用 <code>ngOnInit()</code> 方法。 这里是放置复杂初始化逻辑的好地方。</p><p translation-origin="off">You can count on Angular to call the <code>ngOnInit()</code> method <em>soon</em> after creating the component. That's where the heavy initialization logic belongs.</p><a id="ondestroy"></a><h3 id="ondestroy"><em>OnDestroy()</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#ondestroy"><i class="material-icons">link</i></a></h3><p translation-result="on">一些清理逻辑<em>必须</em>在 Angular 销毁指令之前运行，把它们放在 <code>ngOnDestroy()</code> 中。</p><p translation-origin="off">Put cleanup logic in <code>ngOnDestroy()</code>, the logic that <em>must</em> run before Angular destroys the directive.</p><p translation-result="on">这是在该组件消失之前，可用来通知应用程序中其它部分的最后一个时间点。</p><p translation-origin="off">This is the time to notify another part of the application that the component is going away.</p><p translation-result="on">这里是用来释放那些不会被垃圾收集器自动回收的各类资源的地方。 取消那些对可观察对象和 DOM 事件的订阅。停止定时器。注销该指令曾注册到全局服务或应用级服务中的各种回调函数。 如果不这么做，就会有导致内存泄露的风险。</p><p translation-origin="off">This is the place to free resources that won't be garbage collected automatically. Unsubscribe from Observables and DOM events. Stop interval timers. Unregister all callbacks that this directive registered with global or application services. You risk memory leaks if you neglect to do so.</p><a id="onchanges"></a><h2 id="onchanges"><em>OnChanges()</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#onchanges"><i class="material-icons">link</i></a></h2><p translation-result="on">一旦检测到该组件(或指令)的<strong><em>输入属性</em></strong>发生了变化，Angular 就会调用它的 <code>ngOnChanges()</code> 方法。 本例监控 <code><a href="api/core/OnChanges" class="code-anchor">OnChanges</a></code> 钩子。</p><p translation-origin="off">Angular calls its <code>ngOnChanges()</code> method whenever it detects changes to <strong><em>input properties</em></strong> of the component (or directive). This example monitors the <code><a href="api/core/OnChanges" class="code-anchor">OnChanges</a></code> hook.</p><code-example path="lifecycle-hooks/src/app/on-changes.component.ts" region="ng-on-changes" header="on-changes.component.ts (excerpt)" ng-version="9.0.0-rc.11"><div style="display:none">ngOnChanges(<a href="api/core/QueryList#changes" class="code-anchor">changes</a>: <a href="api/core/SimpleChanges" class="code-anchor">SimpleChanges</a>) { for (let propName in <a href="api/core/QueryList#changes" class="code-anchor">changes</a>) { let chng = <a href="api/core/QueryList#changes" class="code-anchor">changes</a>[propName]; let cur = JSON.stringify(chng.currentValue); let prev = JSON.stringify(chng.previousValue); this.changeLog.push(`${propName}: currentValue = ${cur}, previousValue = ${prev}`); } }</div><header class="ng-star-inserted">on-changes.component.ts (excerpt)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from on-changes.component.ts (excerpt)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="pln">ngOnChanges</span><span class="pun">(</span><a href="api/core/QueryList#changes" class="code-anchor"><span class="pln">changes</span></a><span class="pun">:</span><span class="pln"> </span><a href="api/core/SimpleChanges" class="code-anchor"><span class="typ">SimpleChanges</span></a><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">let</span><span class="pln"> propName </span><span class="kwd">in</span><span class="pln"> </span><a href="api/core/QueryList#changes" class="code-anchor"><span class="pln">changes</span></a><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">let</span><span class="pln"> chng </span><span class="pun">=</span><span class="pln"> </span><a href="api/core/QueryList#changes" class="code-anchor"><span class="pln">changes</span></a><span class="pun">[</span><span class="pln">propName</span><span class="pun">];</span><span class="pln">
    </span><span class="kwd">let</span><span class="pln"> cur  </span><span class="pun">=</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">chng</span><span class="pun">.</span><span class="pln">currentValue</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">let</span><span class="pln"> prev </span><span class="pun">=</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">chng</span><span class="pun">.</span><span class="pln">previousValue</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">changeLog</span><span class="pun">.</span><span class="pln">push</span><span class="pun">(</span><span class="str">`${propName}: currentValue = ${cur}, previousValue = ${prev}`</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><code>ngOnChanges()</code> 方法获取了一个对象，它把每个发生变化的属性名都映射到了一个<a href="api/core/SimpleChange">SimpleChange</a>对象， 该对象中有属性的当前值和前一个值。这个钩子会在这些发生了变化的属性上进行迭代，并记录它们。</p><p translation-origin="off">The <code>ngOnChanges()</code> method takes an object that maps each changed property name to a <a href="api/core/SimpleChange">SimpleChange</a> object holding the current and previous property values. This hook iterates over the changed properties and logs them.</p><p translation-result="on">这个例子中的 <code>OnChangesComponent</code> 组件有两个输入属性：<code>hero</code> 和 <code>power</code>。</p><p translation-origin="off">The example component, <code>OnChangesComponent</code>, has two input properties: <code>hero</code> and <code>power</code>.</p><code-example path="lifecycle-hooks/src/app/on-changes.component.ts" region="inputs" header="src/app/on-changes.component.ts" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Input" class="code-anchor">Input</a>() hero: Hero; @<a href="api/core/Input" class="code-anchor">Input</a>() power: string;</div><header class="ng-star-inserted">src/app/on-changes.component.ts</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/on-changes.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Input" class="code-anchor"><span class="lit">Input</span></a><span class="pun">()</span><span class="pln"> hero</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">;</span><span class="pln">
</span><span class="lit">@</span><a href="api/core/Input" class="code-anchor"><span class="lit">Input</span></a><span class="pun">()</span><span class="pln"> power</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span></code>
    </pre></aio-code></code-example><p translation-result="on">宿主 <code>OnChangesParentComponent</code> 绑定了它们，就像这样：</p><p translation-origin="off">The host <code>OnChangesParentComponent</code> binds to them like this:</p><code-example path="lifecycle-hooks/src/app/on-changes-parent.component.html" region="on-changes" header="src/app/on-changes-parent.component.html" ng-version="9.0.0-rc.11"><div style="display:none">&lt;on-changes [hero]="hero" [power]="power"&gt;&lt;/on-changes&gt;</div><header class="ng-star-inserted">src/app/on-changes-parent.component.html</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/on-changes-parent.component.html">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;on-changes</span><span class="pln"> [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="pln"> [</span><span class="atn">power</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"power"</span><span class="tag">&gt;&lt;/on-changes&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">下面是此例子中的当用户做出更改时的操作演示：</p><p translation-origin="off">Here's the sample in action as the user makes changes.</p><div class="lightbox"><img src="generated/images/guide/lifecycle-hooks/on-changes-anim.gif" alt="OnChanges" width="632" height="512"></div><p translation-result="on">当 <em>power</em> 属性的字符串值变化时，相应的日志就出现了。 但是 <code>ngOnChanges</code> 并没有捕捉到 <code>hero.name</code> 的变化。 这是第一个意外。</p><p translation-origin="off">The log entries appear as the string value of the <em>power</em> property changes. But the <code>ngOnChanges</code> does not catch changes to <code>hero.name</code> That's surprising at first.</p><p translation-result="on">Angular 只会在输入属性的值变化时调用这个钩子。 而 <code>hero</code> 属性的值是一个<em>到英雄对象的引用</em>。 Angular 不会关注这个英雄对象的 <code>name</code> 属性的变化。 这个英雄对象的<em>引用</em>没有发生变化，于是从 Angular 的视角看来，也就没有什么需要报告的变化了。</p><p translation-origin="off">Angular only calls the hook when the value of the input property changes. The value of the <code>hero</code> property is the <em>reference to the hero object</em>. Angular doesn't care that the hero's own <code>name</code> property changed. The hero object <em>reference</em> didn't change so, from Angular's perspective, there is no change to report!</p><a id="docheck"></a><h2 id="docheck"><em>DoCheck()</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#docheck"><i class="material-icons">link</i></a></h2><p translation-result="on">使用 <code><a href="api/core/DoCheck" class="code-anchor">DoCheck</a></code> 钩子来检测那些 Angular 自身无法捕获的变更并采取行动。</p><p translation-origin="off">Use the <code><a href="api/core/DoCheck" class="code-anchor">DoCheck</a></code> hook to detect and act upon changes that Angular doesn't catch on its own.</p><div class="alert is-helpful"><p translation-result="on">用这个方法来检测那些被 Angular 忽略的更改。</p><p translation-origin="off">Use this method to detect a change that Angular overlooked.</p></div><p translation-result="on"><em>DoCheck</em> 范例通过下面的 <code>ngDoCheck()</code> 钩子扩展了 <em>OnChanges</em> 范例：</p><p translation-origin="off">The <em>DoCheck</em> sample extends the <em>OnChanges</em> sample with the following <code>ngDoCheck()</code> hook:</p><code-example path="lifecycle-hooks/src/app/do-check.component.ts" region="ng-do-check" header="DoCheckComponent (ngDoCheck)" ng-version="9.0.0-rc.11"><div style="display:none">ngDoCheck() { if (this.hero.name !== this.oldHeroName) { this.changeDetected = true; this.changeLog.push(`<a href="api/core/DoCheck" class="code-anchor">DoCheck</a>: Hero name changed to "${this.hero.name}" from "${this.oldHeroName}"`); this.oldHeroName = this.hero.name; } if (this.power !== this.oldPower) { this.changeDetected = true; this.changeLog.push(`<a href="api/core/DoCheck" class="code-anchor">DoCheck</a>: Power changed to "${this.power}" from "${this.oldPower}"`); this.oldPower = this.power; } if (this.changeDetected) { this.noChangeCount = 0; } else { // <a href="api/animations/browser/testing/MockAnimationDriver#log" class="code-anchor">log</a> that hook was called when there was no relevant change. let <a href="api/common/NgForOfContext#count" class="code-anchor">count</a> = this.noChangeCount += 1; let noChangeMsg = `<a href="api/core/DoCheck" class="code-anchor">DoCheck</a> called ${<a href="api/common/NgForOfContext#count" class="code-anchor">count</a>}x when no change to hero or power`; if (<a href="api/common/NgForOfContext#count" class="code-anchor">count</a> === 1) { // add new "no change" <a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a> this.changeLog.push(noChangeMsg); } else { // <a href="api/forms/NgModel#update" class="code-anchor">update</a> last "no change" <a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a> this.changeLog[this.changeLog.length - 1] = noChangeMsg; } } this.changeDetected = false; }</div><header class="ng-star-inserted">DoCheckComponent (ngDoCheck)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from DoCheckComponent (ngDoCheck)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="pln">ngDoCheck</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hero</span><span class="pun">.</span><span class="pln">name </span><span class="pun">!==</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">oldHeroName</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">changeDetected </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">changeLog</span><span class="pun">.</span><span class="pln">push</span><span class="pun">(</span><span class="str">`</span><a href="api/core/DoCheck" class="code-anchor"><span class="str">DoCheck</span></a><span class="str">: Hero name changed to "${this.hero.name}" from "${this.oldHeroName}"`</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">oldHeroName </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hero</span><span class="pun">.</span><span class="pln">name</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">power </span><span class="pun">!==</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">oldPower</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">changeDetected </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">changeLog</span><span class="pun">.</span><span class="pln">push</span><span class="pun">(</span><span class="str">`</span><a href="api/core/DoCheck" class="code-anchor"><span class="str">DoCheck</span></a><span class="str">: Power changed to "${this.power}" from "${this.oldPower}"`</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">oldPower </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">power</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">changeDetected</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">noChangeCount </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="com">// </span><a href="api/animations/browser/testing/MockAnimationDriver#log" class="code-anchor"><span class="com">log</span></a><span class="com"> that hook was called when there was no relevant change.</span><span class="pln">
      </span><span class="kwd">let</span><span class="pln"> </span><a href="api/common/NgForOfContext#count" class="code-anchor"><span class="pln">count</span></a><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">noChangeCount </span><span class="pun">+=</span><span class="pln"> </span><span class="lit">1</span><span class="pun">;</span><span class="pln">
      </span><span class="kwd">let</span><span class="pln"> noChangeMsg </span><span class="pun">=</span><span class="pln"> </span><span class="str">`</span><a href="api/core/DoCheck" class="code-anchor"><span class="str">DoCheck</span></a><span class="str"> called ${</span><a href="api/common/NgForOfContext#count" class="code-anchor"><span class="str">count</span></a><span class="str">}x when no change to hero or power`</span><span class="pun">;</span><span class="pln">
      </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><a href="api/common/NgForOfContext#count" class="code-anchor"><span class="pln">count</span></a><span class="pln"> </span><span class="pun">===</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="com">// add new "no change" </span><a href="api/common/http/HttpErrorResponse#message" class="code-anchor"><span class="com">message</span></a><span class="pln">
        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">changeLog</span><span class="pun">.</span><span class="pln">push</span><span class="pun">(</span><span class="pln">noChangeMsg</span><span class="pun">);</span><span class="pln">
      </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="com">// </span><a href="api/forms/NgModel#update" class="code-anchor"><span class="com">update</span></a><span class="com"> last "no change" </span><a href="api/common/http/HttpErrorResponse#message" class="code-anchor"><span class="com">message</span></a><span class="pln">
        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">changeLog</span><span class="pun">[</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">changeLog</span><span class="pun">.</span><span class="pln">length </span><span class="pun">-</span><span class="pln"> </span><span class="lit">1</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> noChangeMsg</span><span class="pun">;</span><span class="pln">
      </span><span class="pun">}</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">changeDetected </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">该代码检测一些<strong>相关的值</strong>，捕获当前值并与以前的值进行比较。 当英雄或它的超能力发生了非实质性改变时，就会往日志中写一条特殊的消息。 这样你可以看到 <code><a href="api/core/DoCheck" class="code-anchor">DoCheck</a></code> 被调用的频率。结果非常显眼：</p><p translation-origin="off">This code inspects certain <em>values of interest</em>, capturing and comparing their current state against previous values. It writes a special message to the log when there are no substantive changes to the <code>hero</code> or the <code>power</code> so you can see how often <code><a href="api/core/DoCheck" class="code-anchor">DoCheck</a></code> is called. The results are illuminating:</p><div class="lightbox"><img src="generated/images/guide/lifecycle-hooks/do-check-anim.gif" alt="DoCheck" width="632" height="588"></div><p translation-result="on">虽然 <code>ngDoCheck()</code> 钩子可以可以监测到英雄的 <code>name</code> 什么时候发生了变化。但其开销很恐怖。 这个 <code>ngDoCheck</code> 钩子被非常频繁的调用 —— 在<em>每次</em>变更检测周期之后，发生了变化的每个地方都会调它。 在这个例子中，用户还没有做任何操作之前，它就被调用了超过二十次。</p><p translation-origin="off">While the <code>ngDoCheck()</code> hook can detect when the hero's <code>name</code> has changed, it has a frightful cost. This hook is called with enormous frequency—after <em>every</em> change detection cycle no matter where the change occurred. It's called over twenty times in this example before the user can do anything.</p><p translation-result="on">大部分检查的第一次调用都是在 Angular 首次渲染该页面中<em>其它不相关数据</em>时触发的。 仅仅把鼠标移到其它 <code>&lt;input&gt;</code> 中就会触发一次调用。 只有相对较少的调用才是由于对相关数据的修改而触发的。 显然，我们的实现必须非常轻量级，否则将损害用户体验。</p><p translation-origin="off">Most of these initial checks are triggered by Angular's first rendering of <em>unrelated data elsewhere on the page</em>. Mere mousing into another <code>&lt;input&gt;</code> triggers a call. Relatively few calls reveal actual changes to pertinent data. Clearly our implementation must be very lightweight or the user experience suffers.</p><a id="afterview"></a><h2 id="afterview">AfterView<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#afterview"><i class="material-icons">link</i></a></h2><p translation-result="on"><em>AfterView</em> 例子展示了 <code><a href="api/core/AfterViewInit" class="code-anchor">AfterViewInit</a>()</code> 和 <code><a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a>()</code> 钩子，Angular 会在每次创建了组件的子视图后调用它们。</p><p translation-origin="off">The <em>AfterView</em> sample explores the <code><a href="api/core/AfterViewInit" class="code-anchor">AfterViewInit</a>()</code> and <code><a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a>()</code> hooks that Angular calls <em>after</em> it creates a component's child views.</p><p translation-result="on">下面是一个子视图，它用来把英雄的名字显示在一个 <code>&lt;input&gt;</code> 中：</p><p translation-origin="off">Here's a child view that displays a hero's name in an <code>&lt;input&gt;</code>:</p><code-example path="lifecycle-hooks/src/app/after-view.component.ts" region="child-view" header="ChildComponent" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'app-child-view', template: '&lt;input [(<a href="api/forms/NgModel" class="code-anchor">ngModel</a>)]="hero"&gt;' }) export class ChildViewComponent { hero = 'Magneta'; }</div><header class="ng-star-inserted">ChildComponent</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from ChildComponent">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'app-child-view'</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">'&lt;input [(</span><a href="api/forms/NgModel" class="code-anchor"><span class="str">ngModel</span></a><span class="str">)]="hero"&gt;'</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ChildViewComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  hero </span><span class="pun">=</span><span class="pln"> </span><span class="str">'Magneta'</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on"><code>AfterViewComponent</code> 把这个子视图显示<em>在它的模板中</em>：</p><p translation-origin="off">The <code>AfterViewComponent</code> displays this child view <em>within its template</em>:</p><code-example path="lifecycle-hooks/src/app/after-view.component.ts" region="template" header="AfterViewComponent (template)" ng-version="9.0.0-rc.11"><div style="display:none">template: ` &lt;div&gt;-- child view begins --&lt;/div&gt; &lt;app-child-view&gt;&lt;/app-child-view&gt; &lt;div&gt;-- child view ends --&lt;/div&gt;`</div><header class="ng-star-inserted">AfterViewComponent (template)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from AfterViewComponent (template)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`
  &lt;div&gt;-- child view begins --&lt;/div&gt;
    &lt;app-child-view&gt;&lt;/app-child-view&gt;
  &lt;div&gt;-- child view ends --&lt;/div&gt;`</span></code>
    </pre></aio-code></code-example><p translation-result="on">下列钩子基于<em>子视图中</em>的每一次数据变更采取行动，它只能通过带<a href="api/core/ViewChild">@ViewChild</a>装饰器的属性来访问子视图。</p><p translation-origin="off">The following hooks take action based on changing values <em>within the child view</em>, which can only be reached by querying for the child view via the property decorated with <a href="api/core/ViewChild">@ViewChild</a>.</p><code-example path="lifecycle-hooks/src/app/after-view.component.ts" region="hooks" header="AfterViewComponent (class excerpts)" ng-version="9.0.0-rc.11"><div style="display:none">export class AfterViewComponent implements <a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a>, <a href="api/core/AfterViewInit" class="code-anchor">AfterViewInit</a> { private prevHero = ''; // <a href="api/core/Query" class="code-anchor">Query</a> for a VIEW child of type `ChildViewComponent` @<a href="api/core/ViewChild" class="code-anchor">ViewChild</a>(ChildViewComponent) viewChild: ChildViewComponent; ngAfterViewInit() { // viewChild is set after the view has been initialized this.logIt('<a href="api/core/AfterViewInit" class="code-anchor">AfterViewInit</a>'); this.doSomething(); } ngAfterViewChecked() { // viewChild is updated after the view has been checked if (this.prevHero === this.viewChild.hero) { this.logIt('<a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a> (no change)'); } else { this.prevHero = this.viewChild.hero; this.logIt('<a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a>'); this.doSomething(); } } // ... }</div><header class="ng-star-inserted">AfterViewComponent (class excerpts)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from AfterViewComponent (class excerpts)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AfterViewComponent</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln">  </span><a href="api/core/AfterViewChecked" class="code-anchor"><span class="typ">AfterViewChecked</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/AfterViewInit" class="code-anchor"><span class="typ">AfterViewInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> prevHero </span><span class="pun">=</span><span class="pln"> </span><span class="str">''</span><span class="pun">;</span><span class="pln">

  </span><span class="com">// </span><a href="api/core/Query" class="code-anchor"><span class="com">Query</span></a><span class="com"> for a VIEW child of type `ChildViewComponent`</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/ViewChild" class="code-anchor"><span class="lit">ViewChild</span></a><span class="pun">(</span><span class="typ">ChildViewComponent</span><span class="pun">)</span><span class="pln"> viewChild</span><span class="pun">:</span><span class="pln"> </span><span class="typ">ChildViewComponent</span><span class="pun">;</span><span class="pln">

  ngAfterViewInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// viewChild is set after the view has been initialized</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logIt</span><span class="pun">(</span><span class="str">'</span><a href="api/core/AfterViewInit" class="code-anchor"><span class="str">AfterViewInit</span></a><span class="str">'</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">doSomething</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  ngAfterViewChecked</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// viewChild is updated after the view has been checked</span><span class="pln">
    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">prevHero </span><span class="pun">===</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">viewChild</span><span class="pun">.</span><span class="pln">hero</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logIt</span><span class="pun">(</span><span class="str">'</span><a href="api/core/AfterViewChecked" class="code-anchor"><span class="str">AfterViewChecked</span></a><span class="str"> (no change)'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">prevHero </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">viewChild</span><span class="pun">.</span><span class="pln">hero</span><span class="pun">;</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logIt</span><span class="pun">(</span><span class="str">'</span><a href="api/core/AfterViewChecked" class="code-anchor"><span class="str">AfterViewChecked</span></a><span class="str">'</span><span class="pun">);</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">doSomething</span><span class="pun">();</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
  </span><span class="com">// ...</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><a id="wait-a-tick"></a><h3 id="abide-by-the-unidirectional-data-flow-rule" translation-result="on">遵循单向数据流规则<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#abide-by-the-unidirectional-data-flow-rule"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="abide-by-the-unidirectional-data-flow-rule">Abide by the unidirectional data flow rule<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#abide-by-the-unidirectional-data-flow-rule"><i class="material-icons">link</i></a></h3><p translation-result="on">当英雄的名字超过 10 个字符时，<code>doSomething()</code> 方法就会更新屏幕。</p><p translation-origin="off">The <code>doSomething()</code> method updates the screen when the hero name exceeds 10 characters.</p><code-example path="lifecycle-hooks/src/app/after-view.component.ts" region="do-something" header="AfterViewComponent (doSomething)" ng-version="9.0.0-rc.11"><div style="display:none">// This surrogate for real business logic sets the `comment` private doSomething() { let c = this.viewChild.hero.length &gt; 10 ? `That's a long name` : ''; if (c !== this.comment) { // Wait a <a href="api/core/testing/tick" class="code-anchor">tick</a> because the component's view has already been checked this.logger.tick_then(() =&gt; this.comment = c); } }</div><header class="ng-star-inserted">AfterViewComponent (doSomething)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from AfterViewComponent (doSomething)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="com">// This surrogate for real business logic sets the `comment`</span><span class="pln">
</span><span class="kwd">private</span><span class="pln"> doSomething</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">let</span><span class="pln"> c </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">viewChild</span><span class="pun">.</span><span class="pln">hero</span><span class="pun">.</span><span class="pln">length </span><span class="pun">&gt;</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="pun">?</span><span class="pln"> </span><span class="str">`That's a long name`</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">''</span><span class="pun">;</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">c </span><span class="pun">!==</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">comment</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// Wait a </span><a href="api/core/testing/tick" class="code-anchor"><span class="com">tick</span></a><span class="com"> because the component's view has already been checked</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logger</span><span class="pun">.</span><span class="pln">tick_then</span><span class="pun">(()</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">comment </span><span class="pun">=</span><span class="pln"> c</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">为什么在更新 <code>comment</code> 属性之前，<code>doSomething()</code> 方法要等上一拍(tick)？</p><p translation-origin="off">Why does the <code>doSomething()</code> method wait a tick before updating <code>comment</code>?</p><p translation-result="on">Angular 的“单向数据流”规则禁止在一个视图已经被组合好<em>之后</em>再更新视图。 而这两个钩子都是在组件的视图已经被组合好之后触发的。</p><p translation-origin="off">Angular's unidirectional data flow rule forbids updates to the view <em>after</em> it has been composed. Both of these hooks fire <em>after</em> the component's view has been composed.</p><p translation-result="on">如果立即更新组件中被绑定的 <code>comment</code> 属性，Angular 就会抛出一个错误(试试!)。 <code>LoggerService.tick_then()</code> 方法延迟更新日志一个回合（浏览器 JavaScript 周期回合），这样就够了。</p><p translation-origin="off">Angular throws an error if the hook updates the component's data-bound <code>comment</code> property immediately (try it!). The <code>LoggerService.tick_then()</code> postpones the log update for one turn of the browser's JavaScript cycle and that's just long enough.</p><p translation-result="on">这里是 <em>AfterView</em> 的操作演示：</p><p translation-origin="off">Here's <em>AfterView</em> in action:</p><div class="lightbox"><img src="generated/images/guide/lifecycle-hooks/after-view-anim.gif" alt="AfterView" width="520" height="532"></div><p translation-result="on">注意，Angular 会频繁的调用 <code><a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a>()</code>，甚至在并没有需要关注的更改时也会触发。 所以务必把这个钩子方法写得尽可能精简，以免出现性能问题。</p><p translation-origin="off">Notice that Angular frequently calls <code><a href="api/core/AfterViewChecked" class="code-anchor">AfterViewChecked</a>()</code>, often when there are no changes of interest. Write lean hook methods to avoid performance problems.</p><a id="aftercontent"></a><h2 id="aftercontent">AfterContent<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#aftercontent"><i class="material-icons">link</i></a></h2><p translation-result="on"><em>AfterContent</em> 例子展示了 <code><a href="api/core/AfterContentInit" class="code-anchor">AfterContentInit</a>()</code> 和 <code><a href="api/core/AfterContentChecked" class="code-anchor">AfterContentChecked</a>()</code> 钩子，Angular 会在外来内容被投影到组件中<em>之后</em>调用它们。</p><p translation-origin="off">The <em>AfterContent</em> sample explores the <code><a href="api/core/AfterContentInit" class="code-anchor">AfterContentInit</a>()</code> and <code><a href="api/core/AfterContentChecked" class="code-anchor">AfterContentChecked</a>()</code> hooks that Angular calls <em>after</em> Angular projects external content into the component.</p><a id="content-projection"></a><h3 id="content-projection" translation-result="on">内容投影<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#content-projection"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="content-projection">Content projection<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#content-projection"><i class="material-icons">link</i></a></h3><p translation-result="on"><em>内容投影</em>是从组件外部导入 HTML 内容，并把它插入在组件模板中指定位置上的一种途径。</p><p translation-origin="off"><em>Content projection</em> is a way to import HTML content from outside the component and insert that content into the component's template in a designated spot.</p><div class="alert is-helpful"><p translation-result="on">AngularJS 的开发者大概知道一项叫做 <em>transclusion</em> 的技术，对，这就是它的马甲。</p><p translation-origin="off">AngularJS developers know this technique as <em>transclusion</em>.</p></div><p translation-result="on">对比<a href="guide/lifecycle-hooks#afterview">前面的 AfterView</a> 例子考虑这个变化。 这次不再通过模板来把子视图包含进来，而是改为从 <code>AfterContentComponent</code> 的父组件中导入它。下面是父组件的模板：</p><p translation-origin="off">Consider this variation on the <a href="guide/lifecycle-hooks#afterview">previous <em>AfterView</em></a> example. This time, instead of including the child view within the template, it imports the content from the <code>AfterContentComponent</code>'s parent. Here's the parent's template:</p><code-example path="lifecycle-hooks/src/app/after-content.component.ts" region="parent-template" header="AfterContentParentComponent (template excerpt)" ng-version="9.0.0-rc.11"><div style="display:none">`&lt;after-content&gt; &lt;app-child&gt;&lt;/app-child&gt; &lt;/after-content&gt;`</div><header class="ng-star-inserted">AfterContentParentComponent (template excerpt)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from AfterContentParentComponent (template excerpt)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="str">`&lt;after-content&gt;
   &lt;app-child&gt;&lt;/app-child&gt;
 &lt;/after-content&gt;`</span></code>
    </pre></aio-code></code-example><p translation-result="on">注意，<code>&lt;app-child&gt;</code> 标签被包含在 <code>&lt;after-content&gt;</code> 标签中。 永远不要在组件标签的内部放任何内容 —— <em>除非你想把这些内容投影进这个组件中</em>。</p><p translation-origin="off">Notice that the <code>&lt;app-child&gt;</code> tag is tucked between the <code>&lt;after-content&gt;</code> tags. Never put content between a component's element tags <em>unless you intend to project that content into the component</em>.</p><p translation-result="on">现在来看该组件的模板：</p><p translation-origin="off">Now look at the component's template:</p><code-example path="lifecycle-hooks/src/app/after-content.component.ts" region="template" header="AfterContentComponent (template)" ng-version="9.0.0-rc.11"><div style="display:none">template: ` &lt;div&gt;-- projected content begins --&lt;/div&gt; &lt;ng-content&gt;&lt;/ng-content&gt; &lt;div&gt;-- projected content ends --&lt;/div&gt;`</div><header class="ng-star-inserted">AfterContentComponent (template)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from AfterContentComponent (template)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">template</span><span class="pun">:</span><span class="pln"> </span><span class="str">`
  &lt;div&gt;-- projected content begins --&lt;/div&gt;
    &lt;ng-content&gt;&lt;/ng-content&gt;
  &lt;div&gt;-- projected content ends --&lt;/div&gt;`</span></code>
    </pre></aio-code></code-example><p translation-result="on"><code>&lt;ng-content&gt;</code> 标签是外来内容的<em>占位符</em>。 它告诉 Angular 在哪里插入这些外来内容。 在这里，被投影进去的内容就是来自父组件的 <code>&lt;app-child&gt;</code> 标签。</p><p translation-origin="off">The <code>&lt;ng-content&gt;</code> tag is a <em>placeholder</em> for the external content. It tells Angular where to insert that content. In this case, the projected content is the <code>&lt;app-child&gt;</code> from the parent.</p><div class="lightbox"><img src="generated/images/guide/lifecycle-hooks/projected-child-view.png" alt="Projected Content" width="230" height="89"></div><div class="alert is-helpful"><p translation-result="on">下列迹象表明存在着<em>内容投影</em>：</p><p translation-origin="off">The telltale signs of <em>content projection</em> are twofold:</p><ul><li><p translation-result="on">在组件的元素标签中有 HTML</p><p translation-origin="off">HTML between component element tags.</p></li><li><p translation-result="on">组件的模板中出现了 <code>&lt;ng-content&gt;</code> 标签</p><p translation-origin="off">The presence of <code>&lt;ng-content&gt;</code> tags in the component's template.</p></li></ul></div><a id="aftercontent-hooks"></a><h3 id="aftercontent-hooks" translation-result="on">AfterContent 钩子<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#aftercontent-hooks"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="aftercontent-hooks">AfterContent hooks<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#aftercontent-hooks"><i class="material-icons">link</i></a></h3><p translation-result="on"><em>AfterContent</em> 钩子和 <em>AfterView</em> 相似。关键的不同点是子组件的类型不同。</p><p translation-origin="off"><em>AfterContent</em> hooks are similar to the <em>AfterView</em> hooks. The key difference is in the child component.</p><ul><li><p translation-result="on"><em>AfterView</em> 钩子所关心的是 <code><a href="api/core/ViewChildren" class="code-anchor">ViewChildren</a></code>，这些子组件的元素标签会出现在该组件的模板<em>里面</em>。</p><p translation-origin="off">The <em>AfterView</em> hooks concern <code><a href="api/core/ViewChildren" class="code-anchor">ViewChildren</a></code>, the child components whose element tags appear <em>within</em> the component's template.</p></li><li><p translation-result="on"><em>AfterContent</em> 钩子所关心的是 <code><a href="api/core/ContentChildren" class="code-anchor">ContentChildren</a></code>，这些子组件被 Angular 投影进该组件中。</p><p translation-origin="off">The <em>AfterContent</em> hooks concern <code><a href="api/core/ContentChildren" class="code-anchor">ContentChildren</a></code>, the child components that Angular projected into the component.</p></li></ul><p translation-result="on">下列 <em>AfterContent</em> 钩子基于<em>子级内容</em>中值的变化而采取相应的行动，它只能通过带有<a href="api/core/ContentChild">@ContentChild</a>装饰器的属性来查询到“子级内容”。</p><p translation-origin="off">The following <em>AfterContent</em> hooks take action based on changing values in a <em>content child</em>, which can only be reached by querying for them via the property decorated with <a href="api/core/ContentChild">@ContentChild</a>.</p><code-example path="lifecycle-hooks/src/app/after-content.component.ts" region="hooks" header="AfterContentComponent (class excerpts)" ng-version="9.0.0-rc.11"><div style="display:none">export class AfterContentComponent implements <a href="api/core/AfterContentChecked" class="code-anchor">AfterContentChecked</a>, <a href="api/core/AfterContentInit" class="code-anchor">AfterContentInit</a> { private prevHero = ''; comment = ''; // <a href="api/core/Query" class="code-anchor">Query</a> for a CONTENT child of type `ChildComponent` @<a href="api/core/ContentChild" class="code-anchor">ContentChild</a>(ChildComponent) contentChild: ChildComponent; ngAfterContentInit() { // contentChild is set after the content has been initialized this.logIt('<a href="api/core/AfterContentInit" class="code-anchor">AfterContentInit</a>'); this.doSomething(); } ngAfterContentChecked() { // contentChild is updated after the content has been checked if (this.prevHero === this.contentChild.hero) { this.logIt('<a href="api/core/AfterContentChecked" class="code-anchor">AfterContentChecked</a> (no change)'); } else { this.prevHero = this.contentChild.hero; this.logIt('<a href="api/core/AfterContentChecked" class="code-anchor">AfterContentChecked</a>'); this.doSomething(); } } // ... }</div><header class="ng-star-inserted">AfterContentComponent (class excerpts)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from AfterContentComponent (class excerpts)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AfterContentComponent</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><a href="api/core/AfterContentChecked" class="code-anchor"><span class="typ">AfterContentChecked</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/AfterContentInit" class="code-anchor"><span class="typ">AfterContentInit</span></a><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> prevHero </span><span class="pun">=</span><span class="pln"> </span><span class="str">''</span><span class="pun">;</span><span class="pln">
  comment </span><span class="pun">=</span><span class="pln"> </span><span class="str">''</span><span class="pun">;</span><span class="pln">

  </span><span class="com">// </span><a href="api/core/Query" class="code-anchor"><span class="com">Query</span></a><span class="com"> for a CONTENT child of type `ChildComponent`</span><span class="pln">
  </span><span class="lit">@</span><a href="api/core/ContentChild" class="code-anchor"><span class="lit">ContentChild</span></a><span class="pun">(</span><span class="typ">ChildComponent</span><span class="pun">)</span><span class="pln"> contentChild</span><span class="pun">:</span><span class="pln"> </span><span class="typ">ChildComponent</span><span class="pun">;</span><span class="pln">

  ngAfterContentInit</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// contentChild is set after the content has been initialized</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logIt</span><span class="pun">(</span><span class="str">'</span><a href="api/core/AfterContentInit" class="code-anchor"><span class="str">AfterContentInit</span></a><span class="str">'</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">doSomething</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  ngAfterContentChecked</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// contentChild is updated after the content has been checked</span><span class="pln">
    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">prevHero </span><span class="pun">===</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">contentChild</span><span class="pun">.</span><span class="pln">hero</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logIt</span><span class="pun">(</span><span class="str">'</span><a href="api/core/AfterContentChecked" class="code-anchor"><span class="str">AfterContentChecked</span></a><span class="str"> (no change)'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">prevHero </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">contentChild</span><span class="pun">.</span><span class="pln">hero</span><span class="pun">;</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">logIt</span><span class="pun">(</span><span class="str">'</span><a href="api/core/AfterContentChecked" class="code-anchor"><span class="str">AfterContentChecked</span></a><span class="str">'</span><span class="pun">);</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">doSomething</span><span class="pun">();</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
  </span><span class="com">// ...</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><a id="no-unidirectional-flow-worries"></a><h3 id="no-unidirectional-flow-worries-with-aftercontent" translation-result="on">使用 <strong>AfterContent</strong> 时，无需担心单向数据流规则<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#no-unidirectional-flow-worries-with-aftercontent"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="no-unidirectional-flow-worries-with-aftercontent">No unidirectional flow worries with <em>AfterContent</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/lifecycle-hooks#no-unidirectional-flow-worries-with-aftercontent"><i class="material-icons">link</i></a></h3><p translation-result="on">该组件的 <code>doSomething()</code> 方法立即更新了组件被绑定的 <code>comment</code> 属性。 它<a href="guide/lifecycle-hooks#wait-a-tick">不用等</a>下一回合。</p><p translation-origin="off">This component's <code>doSomething()</code> method update's the component's data-bound <code>comment</code> property immediately. There's no <a href="guide/lifecycle-hooks#wait-a-tick">need to wait</a>.</p><p translation-result="on">回忆一下，Angular 在每次调用 <em>AfterView</em> 钩子之前也会同时调用 <em>AfterContent</em>。 Angular 在完成当前组件的视图合成之前，就已经完成了被投影内容的合成。 所以你仍然有机会去修改那个视图。</p><p translation-origin="off">Recall that Angular calls both <em>AfterContent</em> hooks before calling either of the <em>AfterView</em> hooks. Angular completes composition of the projected content <em>before</em> finishing the composition of this component's view. There is a small window between the <code>AfterContent...</code> and <code>AfterView...</code> hooks to modify the host view.</p></div></div></aio-doc-viewer></main></mat-sidenav-content></mat-sidenav-container><div class="toc-container no-print ng-star-inserted"><aio-lazy-ce selector="aio-toc"><aio-toc ng-version="9.0.0-rc.11"><div class="toc-inner no-print collapsed ng-star-inserted"><ul class="toc-list"><li class="h1 ng-star-inserted active" title="生命周期钩子"><a href="guide/lifecycle-hooks#lifecycle-hooks">生命周期钩子</a></li><li class="h2 ng-star-inserted" title="组件生命周期钩子概览"><a href="guide/lifecycle-hooks#component-lifecycle-hooks-overview">组件生命周期钩子概览</a></li><li class="h2 ng-star-inserted" title="生命周期的顺序"><a href="guide/lifecycle-hooks#lifecycle-sequence">生命周期的顺序</a></li><li class="h2 ng-star-inserted" title="接口是可选的（严格来说）"><a href="guide/lifecycle-hooks#interfaces-are-optional-technically">接口是可选的（严格来说）</a></li><li class="h2 ng-star-inserted" title="其它 Angular 生命周期钩子"><a href="guide/lifecycle-hooks#other-angular-lifecycle-hooks">其它 Angular 生命周期钩子</a></li><li class="h2 ng-star-inserted" title="生命周期范例"><a href="guide/lifecycle-hooks#lifecycle-examples">生命周期范例</a></li><li class="h2 ng-star-inserted" title="Peek-a-boo：全部钩子"><a href="guide/lifecycle-hooks#peek-a-boo-all-hooks">Peek-a-boo：全部钩子</a></li><li class="h2 ng-star-inserted" title="窥探 OnInit 和 OnDestroy"><a href="guide/lifecycle-hooks#spying-oninit-and-ondestroy">窥探 <em>OnInit</em> 和 <em>OnDestroy</em></a></li><li class="h3 ng-star-inserted" title="OnInit()"><a href="guide/lifecycle-hooks#oninit"><em>OnInit()</em></a></li><li class="h3 ng-star-inserted" title="OnDestroy()"><a href="guide/lifecycle-hooks#ondestroy"><em>OnDestroy()</em></a></li><li class="h2 ng-star-inserted" title="OnChanges()"><a href="guide/lifecycle-hooks#onchanges"><em>OnChanges()</em></a></li><li class="h2 ng-star-inserted" title="DoCheck()"><a href="guide/lifecycle-hooks#docheck"><em>DoCheck()</em></a></li><li class="h2 ng-star-inserted" title="AfterView"><a href="guide/lifecycle-hooks#afterview">AfterView</a></li><li class="h3 ng-star-inserted" title="遵循单向数据流规则"><a href="guide/lifecycle-hooks#abide-by-the-unidirectional-data-flow-rule">遵循单向数据流规则</a></li><li class="h2 ng-star-inserted" title="AfterContent"><a href="guide/lifecycle-hooks#aftercontent">AfterContent</a></li><li class="h3 ng-star-inserted" title="内容投影"><a href="guide/lifecycle-hooks#content-projection">内容投影</a></li><li class="h3 ng-star-inserted" title="AfterContent 钩子"><a href="guide/lifecycle-hooks#aftercontent-hooks">AfterContent 钩子</a></li><li class="h3 ng-star-inserted" title="使用 AfterContent 时，无需担心单向数据流规则"><a href="guide/lifecycle-hooks#no-unidirectional-flow-worries-with-aftercontent">使用 <strong>AfterContent</strong> 时，无需担心单向数据流规则</a></li></ul></div></aio-toc></aio-lazy-ce></div><footer class="no-print"><aio-footer><div class="grid-fluid"><div class="footer-block ng-star-inserted"><h3>资源</h3><ul><li class="ng-star-inserted"><a class="link" href="about" title="Angular 贡献者。">关于</a></li><li class="ng-star-inserted"><a class="link" href="resources" title="网络上的 Angular 工具、培训、博客等">资源列表</a></li><li class="ng-star-inserted"><a class="link" href="presskit" title="我们的联系方式、LOGO 和品牌">宣传资料</a></li><li class="ng-star-inserted"><a class="link" href="https://blog.angular.io/" title="Angular 官方博客">博客</a></li><li class="ng-star-inserted"><a class="link" href="analytics" title="Angular 使用情况分析">使用情况分析</a></li></ul></div><div class="footer-block ng-star-inserted"><h3>帮助</h3><ul><li class="ng-star-inserted"><a class="link" href="https://stackoverflow.com/questions/tagged/angular" title="Stack Overflow: 这里的社区会回答你关于 Angular 的技术问题">Stack Overflow</a></li><li class="ng-star-inserted"><a class="link" href="https://gitter.im/angular/angular" title="和老鸟聊 Angular">Gitter</a></li><li class="ng-star-inserted"><a class="link" href="https://github.com/angular/angular/issues" title="在 github 上报告问题和建议。">报告问题</a></li><li class="ng-star-inserted"><a class="link" href="https://github.com/angular/code-of-conduct/blob/master/CODE_OF_CONDUCT.md" title="让我们彼此尊重">行为规范</a></li></ul></div><div class="footer-block ng-star-inserted"><h3>社区</h3><ul><li class="ng-star-inserted"><a class="link" href="events" title="Angular events around the world.">活动</a></li><li class="ng-star-inserted"><a class="link" href="http://www.meetup.com/topics/angularjs/" title="参加聚会，向别的开发人员学习">聚会</a></li><li class="ng-star-inserted"><a class="link" href="https://twitter.com/angular" title="Twitter">Twitter</a></li><li class="ng-star-inserted"><a class="link" href="https://github.com/angular/angular" title="GitHub">GitHub</a></li><li class="ng-star-inserted"><a class="link" href="contribute" title="向 Angular 做贡献">做贡献</a></li></ul></div><div class="footer-block ng-star-inserted"><h3>多语言</h3><ul><li class="ng-star-inserted"><a class="link" href="https://angular.io/" title="English Version.">English Version</a></li><li class="ng-star-inserted"><a class="link" href="https://angular.tw/" title="正體中文版">正體中文版</a></li><li class="ng-star-inserted"><a class="link" href="https://angular.jp/" title="日本語版">日本語版</a></li><li class="ng-star-inserted"><a class="link" href="https://angular.kr/" title="한국어">한국어</a></li></ul></div></div><p>Super-powered by Google ©2010-2020. 代码授权方式：<a href="license" title="License text">MIT-style License</a>. 文档授权方式：<a href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>.</p><p>当前版本：9.0.1-build.109+sha.7a49faba.</p></aio-footer></footer><div class="cdk-visually-hidden ng-star-inserted"><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color" aria-hidden="true">&nbsp;</mat-icon></div></aio-shell><noscript><div class="background-sky hero"></div><section id="intro" style="text-shadow:1px 1px #1976d2"><div class="hero-logo"><img src="assets/images/logos/angular/angular.svg" width="250" height="250" alt="Angular"></div><div class="homepage-container"><div class="hero-headline">一套框架，多种平台<br>移动 &amp; 桌面</div></div></section><h2 style="color:red;margin-top:40px;position:relative;text-align:center;text-shadow:1px 1px #fafafa"><b><i>该网站需要浏览器支持 JavaScript</i></b></h2></noscript><script src="runtime-es2015.811b066977719df5af39.js" type="module"></script><script src="runtime-es5.811b066977719df5af39.js" nomodule="" defer=""></script><script src="polyfills-es5.1494ca908613f3429457.js" nomodule="" defer=""></script><script src="polyfills-es2015.9057a05c650bf60ecbf4.js" type="module"></script><script src="main-es2015.4e9aa34b562bab1382ab.js" type="module"></script><script src="main-es5.4e9aa34b562bab1382ab.js" nomodule="" defer=""></script><div class="cdk-live-announcer-element cdk-visually-hidden" aria-atomic="true" aria-live="polite"></div></body></html>